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 have met TRIM in the previous chapter. Here you learn what CLEAN removes, what it leaves alone, and how to check that it worked.
What non-printing characters are
Every character in a cell is stored as a numeric code. Most codes stand for letters, digits, and punctuation. The codes 0 through 31 are different. They are control characters, which were designed to control devices such as printers and terminals, not to be shown on screen. They take up space in the cell's text but display as nothing, or as a small box, or as a line break.
Control characters usually arrive with imported data: text copied from a web page, exported from a database, pasted from a PDF, or received from a system that uses unusual line endings. You often cannot see them. A cell can look correct and still fail a lookup because it contains an invisible character that its match does not have.
The control characters you meet most often are:
| Code | Name | Where it comes from |
|---|---|---|
| 9 | Tab | Text copied from tab-separated files or web tables |
| 10 | Line feed | Alt+Enter inside a cell, or Unix-style line endings |
| 13 | Carriage return | Windows-style line endings, older Mac files |
| 0 to 8, 11, 12, 14 to 31 | Other control codes | Binary data, damaged exports, some legacy systems |
What CLEAN does
CLEAN takes one text argument and returns that text with every character from codes 0 through 31 removed.
=CLEAN(A2)The function does not replace the removed characters with anything. It deletes them. This detail causes the most common mistake with CLEAN, covered below.
You can build a test value to see this for yourself. The CHAR function returns the character for a given code, so you can put a tab inside a string:
="Anna"&CHAR(9)&"Lee"This displays as Anna Lee with a wide gap, or as AnnaLee depending on the display. Now wrap it in CLEAN:
=CLEAN("Anna"&CHAR(9)&"Lee")The result is AnnaLee. The tab was removed, and nothing took its place, so the two words are now joined.
What CLEAN does not do
CLEAN has a narrow job. It does not change any of these:
- Normal spaces (code 32). Use
TRIMfor those. - Non-breaking spaces (code 160), which come from web pages and look like spaces. They need
SUBSTITUTE, which a later chapter covers. - The delete character (code 127) and some other non-printing Unicode characters, such as codes 129, 141, 143, 144, and 157.
CLEANonly handles codes 0 through 31. - Capitalization. Use
PROPERfor that.
If a cell still behaves strangely after CLEAN, the culprit is probably one of these. The next section shows how to check.
Checking whether CLEAN is needed
Before you clean a column, find out whether it needs cleaning. LEN returns the number of characters in a cell, and CLEAN removes characters, so the difference is the number of control characters removed:
=LEN(A2)-LEN(CLEAN(A2))A result of 0 means the cell has no control characters in codes 0 through 31. A result above 0 means CLEAN will change the cell.
Compare this with the TRIM check from the previous chapter:
=LEN(A2)-LEN(TRIM(A2))Each formula measures a different problem, so a cell can show 0 on one and a positive number on the other. If both show 0 and a lookup still fails, look for a character that neither function touches. The UNICODE function returns the code of the first character of its argument, so you can examine the last character of a cell:
=UNICODE(RIGHT(A2,1))A normal letter gives a code of 65 or higher. A result of 160 means a non-breaking space. A result below 32 means a control character at the end that CLEAN will remove. To inspect a character in the middle of a cell, use MID with a position:
=UNICODE(MID(A2,5,1))This returns the code of the fifth character. You can change the 5 and fill down to step through the cell.
UNICODE is the better function here. The older CODE function returns codes that depend on the computer's character set, so characters outside basic English may give different numbers on different systems. UNICODE gives consistent numbers.
A worked example
Suppose you imported a list of product names into column A. Some cells contain tabs, and a lookup against a clean product list fails for those rows.
- In
B1, type the headingDiagnosis. InB2, enter=LEN(A2)-LEN(CLEAN(A2))and fill it down the column. - Filter column B for values above 0. These are the rows with control characters.
- In
C1, type the headingCleaned. InC2, enter=CLEAN(A2)and fill down. - Compare the results to your lookup list.
Column C is a helper column, as covered earlier. The original data in column A stays untouched, so you can compare before and after and recover if something goes wrong.
The following table shows what you might see for four rows. The third column shows the count from the diagnosis formula.
| Original (A) | Cleaned (C) | Characters removed (B) |
|---|---|---|
| Widget | Widget | 0 |
| Gadget (tab at end) | Gadget | 1 |
| Cable (line feed, then "Black") | CableBlack | 1 |
| Adapter (two tabs inside) | Adapter | 2 |
The third row shows the joining problem. The line feed was the only separator between "Cable" and "Black", and removing it fused the words. This outcome is why line breaks need their own step.
Handling line breaks safely
When a cell contains line breaks that separate words, replace the line feed with a space and then clean up the result. The SUBSTITUTE function replaces one piece of text with another:
=TRIM(SUBSTITUTE(A2,CHAR(10)," "))This replaces every line feed with a space, then TRIM reduces any resulting runs of spaces to one. If your data comes from a Windows source, line breaks may be a carriage return and a line feed together (codes 13 and 10). The line feed substitution turns the line feed into a space, but the carriage return remains. To handle both, apply CLEAN after the substitution:
=TRIM(CLEAN(SUBSTITUTE(A2,CHAR(10)," ")))The order matters. SUBSTITUTE runs first, because once CLEAN has deleted the line feed, there is nothing left to replace. Excel evaluates the innermost function first, so the function written deepest in the parentheses runs first.
Where CLEAN fits in a combined formula
You can nest CLEAN with the other functions. This formula from the topic plan removes control characters, then extra spaces, then fixes capitalization:
=PROPER(TRIM(CLEAN(A2)))CLEAN goes innermost because it should run first. If a tab sits between two spaces, TRIM cannot collapse the spaces until the tab is gone. Running CLEAN first ensures that TRIM sees the final text. This formula still does not handle line breaks that separate words or non-breaking spaces, which are the topics of later chapters.
Side effects to know about
CLEAN always returns text. If you apply it to a number such as 42, the result is the text "42", which is left-aligned and does not add in a SUM. This matters when a column mixes codes and numbers. A later chapter shows how to convert such results back to numbers.
CLEAN also leaves an empty cell as an empty string, which shows as blank but is not truly empty. Functions such as COUNTBLANK treat a formula that returns "" as blank, but ISBLANK does not.
Common errors
- Expecting CLEAN to remove spaces. It does not. A cell with trailing spaces and no control characters returns unchanged.
- Cleaning text that depends on line breaks to separate words, and getting joined words.
- Assuming a cell is clean because it looks clean. Always run the diagnosis formula.
- Treating a result of 0 from the
CLEANcheck as proof that nothing is wrong. A non-breaking space (code 160) or a character like code 127 will not be counted. - Overwriting the original column with formulas that refer to it. This creates a circular reference. Keep the original and the cleaned column separate.
Exercise
Cell A2 contains the text "Red" followed by a line feed, then "Blue" (typed with Alt+Enter). You enter =CLEAN(A2) in B2 and get "RedBlue". Write a formula that returns "Red Blue" and explain why CLEAN alone fails.
Show answer
Use =TRIM(CLEAN(SUBSTITUTE(A2,CHAR(10)," "))). CLEAN deletes the line feed (code 10) and puts nothing in its place, so the two words join. SUBSTITUTE must run first to turn the line feed into a space. TRIM then removes any extra spaces, and CLEAN removes any other control characters, such as a carriage return.
Exercise
The formula =LEN(A2)-LEN(CLEAN(A2)) returns 0 for a cell, but a lookup using this cell still fails, and =UNICODE(RIGHT(A2,1)) returns 160. What is the problem, and why did the first formula miss it?
Show answer
The cell ends with a non-breaking space (code 160). CLEAN only removes codes 0 through 31, so it leaves code 160 alone and the length difference is 0. Replace it with a normal space before trimming: =TRIM(SUBSTITUTE(A2,CHAR(160)," ")).