Sub topic 8
Non-breaking spaces: what TRIM misses
This chapter assumes you can enter and copy formulas, use relative references such as A2, and tell text from numbers in a cell. You should also know what TRIM and CLEAN do. Here you will deal with a space character that both of them leave alone.
The problem
TRIM removes the ordinary space, which is character code 32. Text copied from web pages, PDFs, and some databases often contains a non-breaking space instead. It is character code 160, and it looks exactly like a normal space. TRIM does not treat it as a space, and CLEAN does not remove it, because CLEAN only targets codes 0 through 31.
The result is a cell that looks clean but still fails. Lookups return #N/A, and comparisons such as =A2=B2 return FALSE. Sorting and filtering also treat "Smith" and "Smith" followed by a hidden space as different values.
Confirm the cause
Do not guess. First check whether TRIM has anything to remove:
=LEN(A2)-LEN(TRIM(A2))If this returns 0 but the cell still seems to have extra spaces, a character other than code 32 is the cause. Look at the last character:
=UNICODE(RIGHT(A2,1))A result of 160 confirms a trailing non-breaking space. A result of 32 means an ordinary space, and TRIM can handle it. Change RIGHT(A2,1) to LEFT(A2,1) to test the first character. To test a character in the middle, use MID(A2,n,1), where n is its position.
Convert first, then trim
SUBSTITUTE replaces one piece of text with another. Use it to turn each non-breaking space into a normal space. TRIM can then remove it, along with any other extra spaces.
=PROPER(TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," "))))The formula works from the inside out:
SUBSTITUTE(A2,CHAR(160)," ")replaces every code 160 character with a normal space.CLEAN(...)removes control characters.TRIM(...)removes leading and trailing spaces and reduces runs of spaces to one.PROPER(...)standardizes capitalization.
SUBSTITUTE must come before TRIM. If it ran after TRIM, the converted spaces would stay in the text as leading, trailing, or doubled spaces.
Worked example
Suppose A2 holds Ana followed by two non-breaking spaces and LOPEZ, with one more non-breaking space at the end. The following table shows what each formula returns.
| Formula | Result | Why |
|---|---|---|
| `=TRIM(A2)` | Unchanged text with hidden spaces | TRIM ignores code 160 |
| `=LEN(A2)-LEN(TRIM(A2))` | 0 | TRIM found nothing to remove |
| `=UNICODE(RIGHT(A2,1))` | 160 | The last character is a non-breaking space |
| `=TRIM(SUBSTITUTE(A2,CHAR(160)," "))` | Ana LOPEZ | The code 160 characters became normal spaces first |
| `=PROPER(TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," "))))` | Ana Lopez | The full cleanup |
To check that the cleanup worked, compare lengths: =LEN(A2)-LEN(SUBSTITUTE(A2,CHAR(160),"")) counts the non-breaking spaces in the original cell.
Common errors
- Using CHAR instead of UNICODE for diagnosis. CHAR(160) creates the character, and UNICODE reads one. Use
UNICODEto inspect text andCHAR(160)inside SUBSTITUTE. - Replacing with nothing.
SUBSTITUTE(A2,CHAR(160),"")deletes the character. Words that were separated only by a non-breaking space then run together. Replace with" "unless you are sure the character sat at the edge of the text. - Other unusual spaces. Some data contains different characters, such as a zero-width space (code 8203). If UNICODE returns another number, use that number in
CHARorUNICHAR. UseUNICHAR(8203), because CHAR only handles codes up to 255.
Exercise
A2 displays "North Region" but =LEN(A2)-LEN(TRIM(A2)) returns 0, and =A2="North Region" returns FALSE. =UNICODE(RIGHT(A2,1)) returns 160. Write a formula that returns the cleaned text in proper case, and explain why TRIM alone failed.
Show answer
Use =PROPER(TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," ")))). TRIM removes only the ordinary space (code 32). The trailing character is a non-breaking space (code 160), so TRIM saw nothing to remove, and the cell stayed different from "North Region". SUBSTITUTE converts it to a normal space first, so TRIM can then remove it.