Sub topic 7
Finding Hidden Characters with LEN and UNICODE
TRIM and CLEAN fix the problems they know about. When a cell still misbehaves after both, or when you want to know how much cleaning a column needs, you have to look inside the cell. This chapter shows how to use LEN, CODE, and UNICODE to find characters you can't see.
This chapter assumes you can enter and copy formulas, understand relative references such as A2, and know the difference between text and numbers in a cell. It also assumes you have met TRIM and CLEAN. TRIM removes extra spaces, and CLEAN removes the non-printing characters with codes 0 through 31.
Why you need a diagnosis step
Two cells can look identical on screen and still differ. A lookup for "Cara Diaz" fails because the cell holds "Cara Diaz" followed by an invisible character. You can't fix that by looking at it, and you shouldn't guess by wrapping every cell in more functions.
Diagnosis answers three questions:
- Does this cell contain characters that TRIM or CLEAN would remove?
- If I apply TRIM and CLEAN and the cell still looks wrong, which character is left?
- Where in the string is it?
Each question has a formula. Every formula here reads the cell and changes nothing, so you can run them safely next to your data.
LEN: counting characters
LEN returns the number of characters in a cell, and that includes spaces and invisible characters. A cell that shows Eli Kim has a LEN of 7. If LEN says 8, one character is hiding somewhere.
The comparison trick is to measure a cell before and after a cleaning function. The difference is the number of characters the function removed.
=LEN(A2)-LEN(TRIM(A2))A result of 0 means TRIM would change nothing. A result of 2 means TRIM would remove two spaces. TRIM removes leading spaces, trailing spaces, and extra spaces between words. A cell with two spaces between words and no other problem gives 1, because TRIM leaves one space.
The same idea works for CLEAN:
=LEN(A2)-LEN(CLEAN(A2))This counts the control characters (codes 0 through 31) that CLEAN would remove. Tabs, line feeds, and carriage returns are all in that range.
Put these two formulas in helper columns beside your data. Then filter for values above 0 to see which rows need cleaning, and how many.
UNICODE and CODE: naming a character
LEN tells you something is there. UNICODE tells you what it is. It returns the numeric code of the first character of the text you give it, so you pair it with LEFT, RIGHT, or MID to choose which character to inspect.
=UNICODE(RIGHT(A2,1))This returns the code of the last character in the cell. A normal letter gives a value such as 122 for "z". A value of 160 reveals a trailing non-breaking space, which TRIM does not remove.
Use the same pattern for the first character or for any position:
=UNICODE(LEFT(A2,1))
=UNICODE(MID(A2,4,1))The second formula inspects the fourth character.
CODE does the same job but depends on the character set of your system. It is dependable for codes below 128 and can differ between Windows and Mac above that. UNICODE returns the same standard code on both, so prefer it. The matching function that goes the other way is UNICHAR, which turns a code into a character.
Codes worth knowing
| Code | Character | Removed by TRIM? | Removed by CLEAN? |
|---|---|---|---|
| 9 | Tab | No | Yes |
| 10 | Line feed (line break inside a cell) | No | Yes |
| 13 | Carriage return | No | Yes |
| 32 | Normal space | Yes | No |
| 127 | Delete character | No | No |
| 160 | Non-breaking space | No | No |
| 8203 | Zero-width space | No | No |
The last three rows are the troublemakers. TRIM and CLEAN both ignore them, so the diagnosis formulas for those functions report 0 while the cell is still dirty. You fix code 160 with SUBSTITUTE, which the next chapter covers.
A worked example
Suppose A2:A6 holds these values. The characters are described in the first column because you can't see them.
| Cell | Contents | LEN | LEN − LEN(TRIM) | LEN − LEN(CLEAN) | UNICODE(RIGHT) |
|---|---|---|---|---|---|
| A2 | Two leading spaces, then `Ana Lopez` | 11 | 2 | 0 | 122 |
| A3 | `Ben` and `Ito` with two spaces between | 8 | 1 | 0 | 111 |
| A4 | `Cara Diaz` plus a non-breaking space | 10 | 0 | 0 | 160 |
| A5 | `Dev`, a tab, then `Rao` | 7 | 0 | 1 | 111 |
| A6 | `Eli Kim` | 7 | 0 | 0 | 109 |
Read the table row by row:
- A2 and A3 have spaces that TRIM removes. The difference column shows how many.
- A4 is the sneaky one. Both difference columns say 0, yet LEN is 10 for a nine-character name. The last-character check finds code 160, so the extra character is a non-breaking space.
- A5 has a tab, which CLEAN removes. The last-character check says 111, the letter "o", because the tab is in the middle. One position check is not a full scan.
- A6 is clean. Its LEN matches what you can count by eye.
The A4 and A5 rows show why you run several checks. A single check can miss a problem that a different one catches.
Scanning every character in a cell
When the cause isn't at the start or end, list the code of every character. In Excel 365 or Excel 2021, one formula does it:
=UNICODE(MID(A5,SEQUENCE(LEN(A5)),1))SEQUENCE builds the numbers 1 through LEN(A5). MID then extracts each character, and UNICODE converts each to a code. The results spill down a column. For A5 you get 68, 101, 118, 9, 82, 97, 111. These are D, e, v, tab, R, a, o, and the 9 gives away the tab.
In older versions without SEQUENCE, build the same table by hand:
- Type the position numbers 1, 2, 3, and so on across B1:K1.
- In B2, enter the formula below.
- Copy it right across the row.
=IF(B$1>LEN($A5),"",UNICODE(MID($A5,B$1,1)))The IF keeps columns beyond the end of the text blank. Without it, MID returns an empty string and UNICODE returns an error.
Any code outside the ranges you expect is a suspect. Letters and digits sit between 48 and 122. Anything under 32, or equal to 127, 160, or 8203, deserves a closer look.
Counting a specific character
Once you know the culprit's code, you can count how many cells contain it. This formula counts non-breaking spaces in one cell:
=LEN(A2)-LEN(SUBSTITUTE(A2,CHAR(160),""))SUBSTITUTE removes every character 160, and the length difference is the count. You can swap in any code. UNICHAR(160) works the same as CHAR(160) here and avoids character-set differences on some systems.
The same pattern counts line feeds with CHAR(10), which tells you which cells have line breaks.
Choosing a check
The diagram shows a sensible order for a cell that looks wrong.
A cell can follow more than one branch. A cell with leading spaces and a tab shows up in both the TRIM and CLEAN checks.
Errors and edge cases
- UNICODE on an empty cell returns #VALUE!.
RIGHTof an empty cell is an empty string, and UNICODE has no first character to read. Guard it when blanks are possible:=IF(LEN(A2)=0,"",UNICODE(RIGHT(A2,1))). - UNICODE reads only one character. It always looks at the first character of its argument, so you must choose the position with LEFT, RIGHT, or MID.
- Numbers have a LEN too. LEN of the number 1500 is 4, because Excel measures its text form. A number stored as text and a real number can have the same LEN, so LEN cannot tell them apart. Use
=ISTEXT(A2)for that. - A flag based on the three functions can miss problems. Checking whether
LEN(A2)differs fromLEN(PROPER(TRIM(CLEAN(A2))))flags only what those functions remove. PROPER never changes length, and it never sees codes 127, 160, or 8203. Add the SUBSTITUTE count if your data comes from web pages or PDFs, which are the usual source of code 160. - The wrong position gives a misleading "clean" answer. The A5 row above shows this. Check the first character, the last character, and then scan before you decide a cell has no hidden characters.
Practice
Exercise
A cell in A2 shows "Mia Chen" and LEN(A2) returns 9, though "Mia Chen" has 8 characters. LEN(A2)-LEN(TRIM(A2)) returns 0 and LEN(A2)-LEN(CLEAN(A2)) returns 0. Which formula would you use next, and what result would confirm a non-breaking space?
Show answer
Use =UNICODE(RIGHT(A2,1)), and also check =UNICODE(LEFT(A2,1)) if the first check shows nothing unusual. A result of 160 confirms a non-breaking space. TRIM removes only normal spaces (code 32) and CLEAN removes only codes 0 through 31, so both differences are 0.
Exercise
A cell contains the text "Dev", a tab, and "Rao". Which diagnostic formula flags the tab, and why does UNICODE(RIGHT(A5,1)) not find it?
Show answer
=LEN(A5)-LEN(CLEAN(A5)) returns 1, which flags the tab as a control character that CLEAN would remove. UNICODE(RIGHT(A5,1)) looks only at the last character, which is the letter "o" (code 111). The tab is in the middle. =UNICODE(MID(A5,4,1)) returns 9 and identifies the tab.
Exercise
Write a formula that counts the non-breaking spaces in A2, and explain what it does.
Show answer
=LEN(A2)-LEN(SUBSTITUTE(A2,CHAR(160),"")). SUBSTITUTE removes every character with code 160 from a copy of the text, and subtracting the shorter length from the original length gives the number of characters removed.