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. It also assumes you have met TRIM, CLEAN, and PROPER in the earlier chapters. Here you will learn why TRIM sometimes leaves a cell unchanged, how to confirm that a non-breaking space is the cause, and how to remove it.
The symptom
You run =TRIM(A2) on a cell that clearly has a space at the end. The result looks the same, and LEN still reports the same length. A lookup against the cell still returns #N/A.
The cause is usually a non-breaking space. It looks like a normal space but is a different character.
Two characters that look alike
A normal space is character code 32. A non-breaking space is character code 160. Web pages use it as to stop a line from wrapping between two words. When you copy a table from a web page, a PDF, or an email, those characters often come along into Excel.
TRIM removes only code 32. A non-breaking space is not code 32, so TRIM does not see it.
| Behavior | Normal space (32) | Non-breaking space (160) |
|---|---|---|
| Looks like a space | Yes | Yes |
| Counted by `LEN` | Yes | Yes |
| Removed by `TRIM` | Yes | No |
| Collapsed by `TRIM` when repeated | Yes | No |
| Removed by `CLEAN` | No | No |
CLEAN does not help either. It removes only the codes 0 through 31, and 160 is outside that range.
The result is a cell that looks clean but is not:
- Leading or trailing non-breaking spaces stay.
- Two non-breaking spaces between words stay as two.
- Exact-match functions such as
MATCH,VLOOKUP, andXLOOKUPtreat"Ana Lopez"and"Ana Lopez"plus a trailing code 160 as different values.
=MATCH("Ana Lopez",A2,0)If A2 holds Ana Lopez followed by a non-breaking space, this returns #N/A. The visible text is the same, but the cell has one more character.
Confirming the cause
Do not guess. Test for code 160 before you change anything.
The first test is to look at the last character. This formula returns its Unicode code:
=UNICODE(RIGHT(A2,1))A result of 32 means a normal space, which TRIM can handle. A result of 160 confirms a trailing non-breaking space. Use LEFT(A2,1) to check the start of the cell.
The second test counts how many non-breaking spaces a cell contains, wherever they are:
=LEN(A2)-LEN(SUBSTITUTE(A2,UNICHAR(160),""))SUBSTITUTE removes every code 160 from a copy of the text. The difference in length is the number of characters removed. A result of 0 means the cell has none.
This test is more useful than the one from earlier in the tutorial, =LEN(A2)-LEN(TRIM(A2)). That formula counts only what TRIM would remove. For a cell with a trailing non-breaking space it returns 0, which wrongly suggests nothing is wrong.
Removing non-breaking spaces with SUBSTITUTE
SUBSTITUTE replaces specific text inside a string:
SUBSTITUTE(text, old_text, new_text)To convert a non-breaking space into a normal space, replace code 160 with " ":
=SUBSTITUTE(A2,CHAR(160)," ")After this step the cell contains ordinary spaces, so TRIM can remove or collapse them:
=TRIM(SUBSTITUTE(A2,CHAR(160)," "))The order matters. SUBSTITUTE goes inside TRIM because the formula runs from the inside out. If TRIM ran first, it would do nothing to the non-breaking spaces. SUBSTITUTE would then turn them into spaces that nothing removes afterward.
The diagram shows the decision TRIM effectively makes for each space-like character.
Replace with a space, not with nothing
You could replace code 160 with an empty string, "". Do that only when you are sure the character is not separating words. A non-breaking space often sits between a first and last name. Replacing it with nothing turns Ana Lopez into AnaLopez. Replacing it with a normal space keeps the words apart, and TRIM then cleans up any extras.
The full cleanup formula
Add SUBSTITUTE as the innermost step of the combined formula from the previous chapter:
=PROPER(TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," "))))Reading from the inside out:
- SUBSTITUTE converts non-breaking spaces to normal spaces.
- CLEAN removes control characters with codes 0 through 31.
- TRIM removes leading and trailing spaces and collapses repeated spaces between words.
- PROPER standardizes capitalization.
Put the capitalization step last. It does not change which characters are in the cell, so it should not run before the steps that change them.
A worked example
Suppose A2 contains ana + code 160 + LOPEZ + code 160, with the last character at the end of the cell. The visible text is ana LOPEZ, but the cell has 10 characters.
| Formula | Result | `LEN` of result |
|---|---|---|
| `=A2` | `ana LOPEZ` | 10 |
| `=TRIM(A2)` | `ana LOPEZ` | 10 |
| `=SUBSTITUTE(A2,CHAR(160)," ")` | `ana LOPEZ ` | 10 |
| `=TRIM(SUBSTITUTE(A2,CHAR(160)," "))` | `ana LOPEZ` | 9 |
| `=PROPER(TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," "))))` | `Ana Lopez` | 9 |
The second row shows the problem: TRIM changes nothing. The third row shows the SUBSTITUTE step alone, which converts the characters but leaves the trailing space. The fourth row shows TRIM removing that space once it is a normal space. The last row produces the finished text.
CHAR or UNICHAR
The formulas above use CHAR(160), which works in Excel for Windows. CHAR uses the system's character set, and that set differs between platforms. In Excel for Mac, code 160 in that set is not a non-breaking space. The formula would then run without an error and fix nothing.
UNICHAR(160) always returns the Unicode character 160, on every platform. It is available in Excel 2013 and later. If you share workbooks across systems, use it:
=PROPER(TRIM(CLEAN(SUBSTITUTE(A2,UNICHAR(160)," "))))The matching diagnostic is UNICODE, as used earlier, not CODE. CODE has the same platform dependence as CHAR.
Other characters that behave the same way
Code 160 is the most common case, but not the only one. Some imported text contains other characters that look like spaces or that have no visible form at all. TRIM and CLEAN miss all of these as well.
| Code | Name | Typical symptom |
|---|---|---|
| 160 | Non-breaking space | Looks like a space, TRIM does nothing |
| 8239 | Narrow non-breaking space | Looks like a thin space, common in some locale formatting |
| 8203 | Zero-width space | Invisible, adds to `LEN` but shows nothing |
| 12288 | Ideographic space | Wide space from East Asian text |
Find the code with UNICODE, then add one more SUBSTITUTE. For a visible space-like character, convert it to " ". For an invisible one such as the zero-width space, replace it with "" because it never separated words:
=TRIM(SUBSTITUTE(SUBSTITUTE(A2,UNICHAR(160)," "),UNICHAR(8203),""))Add only the substitutions that your data needs. A long chain of SUBSTITUTE calls for characters that never appear makes the formula harder to read and check.
Removing them without a formula
If you only need to fix a column once, Find and Replace is an alternative. In the Find what box, enter a non-breaking space. On Windows you can type it by holding Alt and typing 0160 on the numeric keypad. Alternatively, copy one offending character from a cell and paste it into the box. In Replace with, enter one normal space, or leave it empty for an invisible character. Then choose Replace All.
This changes the cells in place, so keep a copy of the original column first. The formula approach is usually safer because the source data stays intact and you can compare the two columns.
Common errors
- Checking with TRIM only.
=LEN(A2)-LEN(TRIM(A2))returns 0 for non-breaking spaces. Test withUNICODEor the SUBSTITUTE count instead. - Putting SUBSTITUTE outside TRIM. The characters are converted after TRIM has already run, so the result still has stray spaces.
- Replacing with an empty string. This joins words that were separated.
- Using
CHAR(160)on a Mac. It may not match the character. UseUNICHAR(160). - Assuming one pass finds everything. If the result still looks wrong, check the first and last characters again. A different code may be involved.
Exercise
Cell A2 shows Smith with a trailing space. =TRIM(A2) returns text of the same length, and =UNICODE(RIGHT(A2,1)) returns 160. Write one formula that returns Smith with no extra characters, and explain why TRIM alone failed.
Show answer
Use =TRIM(SUBSTITUTE(A2,UNICHAR(160)," ")). TRIM removes only the normal space, character code 32. The trailing character is code 160, a non-breaking space, so TRIM ignores it. SUBSTITUTE first converts code 160 to a normal space, and TRIM then removes that space.
Exercise
A cell contains new + code 160 + york. You use =PROPER(TRIM(SUBSTITUTE(A2,UNICHAR(160),""))). What does it return, and how should you fix it?
Show answer
It returns Newyork, because replacing code 160 with an empty string joins the two words. Replace the character with a normal space instead: =PROPER(TRIM(SUBSTITUTE(A2,UNICHAR(160)," "))). This returns New York.
Exercise
Write a formula that returns how many non-breaking spaces are in A2.
Show answer
=LEN(A2)-LEN(SUBSTITUTE(A2,UNICHAR(160),"")). SUBSTITUTE removes every code 160 from a copy of the text, so the drop in length equals the number of non-breaking spaces. A result of 0 means there are none.