Sub topic 3
Cleaning Text in Excel with TRIM, CLEAN, and PROPER
Assume you can enter and copy formulas, use relative references such as A2, and distinguish text from numbers. This chapter handles characters that the basic cleanup formula may miss.
Non-breaking spaces: what TRIM misses
A non-breaking space is a space character commonly inserted by websites and exported reports. Excel's TRIM removes ordinary extra spaces, but it does not remove character 160.
Check the final character in A2:
=UNICODE(RIGHT(A2,1))If the result is 160, replace it before trimming:
=PROPER(TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," "))))SUBSTITUTE changes each non-breaking space to an ordinary space. CLEAN removes control characters, and TRIM then removes extra spaces.
Line breaks: remove or replace with a space
A line feed has character code 10. Replacing it with a space keeps separate words apart:
=TRIM(SUBSTITUTE(A2,CHAR(10)," "))Using CLEAN(A2) alone removes the line feed but can join words together. Choose replacement when the line break separates words, and removal when it is only unwanted formatting.
Where PROPER gets names and abbreviations wrong
PROPER capitalizes each word, but it can damage intentional capitalization. For example, it may turn API documentation into Api Documentation and it's ready into It'S Ready. Names such as O'Sullivan also need checking.
Use PROPER as a first pass, then review names, abbreviations, product codes, and apostrophes manually. A narrow correction can fix a known case:
=SUBSTITUTE(PROPER(A2),"'S","'s")This correction is not safe for every name.
Replacing formulas with cleaned values
After checking the helper column:
- Select the formula results and copy them.
- Select the destination cells.
- Choose Paste Special, then Values.
The cells keep the cleaned text but no longer depend on the source cells or formulas.
> [!WARNING] > Keep the original column until the cleaned values have been checked. Paste values is difficult to undo after other workbook changes.
Numbers that stay text after cleaning
Text cleanup functions return text. A cleaned value such as "00125" remains text, even if it looks numeric. For arithmetic, convert it explicitly:
=VALUE(TRIM(CLEAN(A2)))Do not convert identifiers with meaningful leading zeros unless that is intended.
Flash Fill and Power Query as alternatives
Use Flash Fill for a small, predictable pattern that you can verify immediately. Use Power Query for repeatable imports, larger datasets, and documented transformation steps. Worksheet formulas are convenient when the data is already in a sheet and the cleanup rules are simple.
Exercise
Which formula replaces non-breaking spaces before cleaning text?
Show answer
=PROPER(TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," "))))