Sub topic 7
Finding hidden characters with LEN and UNICODE
Some cells look clean but still fail a lookup or a comparison. This chapter shows how to find out why before you choose a fix. It assumes you can enter and copy formulas, use relative references such as A2, and tell text from numbers in a cell.
Why diagnose first
TRIM removes extra spaces and CLEAN removes control characters. Neither removes a non-breaking space. If you apply both and the problem stays, you need to see what is in the cell. Two checks do this: compare lengths, then read the character codes.
Step 1: Count the characters TRIM would remove
LEN returns the number of characters in a cell. Comparing it with the length after TRIM shows whether extra spaces exist.
=LEN(A2)-LEN(TRIM(A2))A result of 0 means TRIM has nothing to remove. A result above 0 means the cell has leading, trailing, or repeated spaces.
You can also compare LEN with the length you expect. If a code such as "AB-100" should be 6 characters and LEN returns 7, one character is hidden somewhere.
Step 2: Read the code of a suspect character
When LEN suggests a hidden character but TRIM reports nothing to remove, look at the character itself. UNICODE returns the numeric code of the first character in the text. To inspect the last character, use RIGHT to isolate it.
=UNICODE(RIGHT(A2,1))For a normal trailing letter or digit, you get that character's code. A value of 160 reveals a trailing non-breaking space, which TRIM does not remove.
To inspect the first character, use LEFT in the same way.
=UNICODE(LEFT(A2,1))CODE works like UNICODE but covers only the characters in your system's character set. UNICODE is the safer choice because it handles any character.
Reading the codes
| Code | Character | Removed by TRIM | Removed by CLEAN |
|---|---|---|---|
| 32 | Normal space | Yes | No |
| 160 | Non-breaking space | No | No |
| 9 | Tab | No | Yes |
| 10 | Line feed | No | Yes |
CLEAN removes codes 0 through 31. It does not touch code 160, so that character needs SUBSTITUTE, which a later chapter covers.
The diagnostic flow
Worked example
Suppose A2 holds the text "Smith " and a lookup for "Smith" returns #N/A. You run the checks in B2 and C2.
=LEN(A2)This returns 6, but "Smith" has 5 letters. The extra character is at the end. Now check its code.
=UNICODE(RIGHT(A2,1))This returns 160. TRIM will not help here, because the trailing character is a non-breaking space and not a normal space. You now know the fix needs SUBSTITUTE, not another pass of TRIM.
Common errors
- Checking the wrong end. A hidden character can sit at the start, the end, or in the middle. Test both ends with LEFT and RIGHT first.
- Assuming a result of 0 from the TRIM check means the cell is clean. The check only counts normal spaces. A non-breaking space gives 0 and still breaks lookups.
- Using CODE on unusual characters. CODE can return a wrong or unexpected value for characters outside your system's character set. Use UNICODE.
- Reading a middle character. To inspect position n, use
=UNICODE(MID(A2,n,1)).
Exercise
Cell A2 shows "North" and LEN(A2) returns 6. LEN(A2)-LEN(TRIM(A2)) returns 0, and UNICODE(RIGHT(A2,1)) returns 160. What is the hidden character, and why did the TRIM check return 0?
Show answer
The hidden character is a non-breaking space at the end of the cell. The TRIM check returned 0 because TRIM only removes normal spaces (code 32), so it treats the length as unchanged. The cell needs SUBSTITUTE to replace CHAR(160) with a normal space before TRIM can remove it.
Exercise
Which formula returns the code of the third character in A2?
Show answer
=UNICODE(MID(A2,3,1))