Sub topic 4
CLEAN: removing non-printing characters
This chapter assumes you can enter and copy formulas, use relative references such as A2, and tell text from numbers in a cell. It also assumes you know the helper-column approach from the earlier chapter: you write the formula in a new column beside the raw data and leave the original untouched.
What CLEAN does
Text copied from web pages, databases, and exported files often contains control characters. These are characters with no visible shape that tell software how to behave, such as a tab or a line feed. They can make two cells that look identical fail a comparison or a lookup.
CLEAN removes the characters with codes 0 through 31. These are the first 32 characters of the ASCII table, which are all non-printing. Tabs (code 9) and line feeds (code 10) are the ones you meet most often.
=CLEAN(A2)Fill the formula down the helper column, the same way you did with TRIM.
What CLEAN leaves alone
CLEAN only knows the control characters in that range. It does not change visible characters, spaces, or capitalization. The table shows how it compares with the other two functions.
| Problem in the cell | Fixed by CLEAN? | Fixed by |
|---|---|---|
| Tab or line feed inside the text | Yes | `CLEAN` |
| Extra regular spaces (code 32) | No | `TRIM` |
| Non-breaking space (code 160) | No | `SUBSTITUTE`, then `TRIM` |
| Wrong capitalization | No | `PROPER` |
Because CLEAN ignores code 32, a cell with extra spaces comes back unchanged. Use TRIM for those.
A gotcha: words can run together
CLEAN deletes a control character. It does not replace it with a space. Suppose A2 contains Unit 4, then a line feed, then North. On screen the text sits on two lines. After cleaning, the two parts are joined:
=CLEAN(A2)The result is Unit 4North. If the line feed was acting as a separator, you have lost the boundary between the words. The fix is to replace the line feed with a space before trimming. The line-breaks chapter covers this in detail, but here is the pattern:
=TRIM(SUBSTITUTE(A2,CHAR(10)," "))Use plain CLEAN when the control character is stray debris, such as a tab at the end of a value. Use SUBSTITUTE when the character separated words.
Checking whether CLEAN is needed
You can count how many characters CLEAN would remove by comparing lengths. This is the same idea as the TRIM check you saw earlier.
=LEN(A2)-LEN(CLEAN(A2))A result of 0 means the cell has no characters in the 0 through 31 range. A result above 0 means CLEAN will change it. If the result is 0 but the cell still misbehaves, the problem is probably a character CLEAN does not cover, such as a non-breaking space. A later chapter shows how to identify it with UNICODE.
Text results from CLEAN
CLEAN always returns text. If a cell holds a real number, =CLEAN(A2) gives you the same digits as text, so they are left-aligned and may not add up in a SUM. This only matters for numbers, but it is a common surprise. A later chapter covers converting these back.
Using CLEAN in a combined formula
On its own, CLEAN is one step of a basic cleanup. The usual order is CLEAN, then TRIM, then PROPER, and the next chapters build the nested formula step by step. Since Excel evaluates the innermost function first, CLEAN goes in the middle:
=PROPER(TRIM(CLEAN(A2)))Exercise
Cell A2 contains the text "Red", a line feed, and "Blue". The formula =CLEAN(A2) returns "RedBlue". Write a formula that returns "Red Blue" instead, and explain why CLEAN alone does not work.
Show answer
Use =TRIM(SUBSTITUTE(A2,CHAR(10)," ")). CLEAN removes the line feed (code 10) without putting anything in its place, so the two words join. SUBSTITUTE replaces the line feed with a space first, and TRIM then removes any extra spaces that result.