Remove Hidden Characters in Excel: TRIM Not Working?
- The formula that removes most hidden characters in Excel is
=TRIM(CLEAN(A2)). CLEAN strips control characters with codes 0 to 31, TRIM strips extra normal spaces. - If TRIM is not working, the space is almost always a non-breaking space (code 160) from a web page, PDF or system export. Use
=TRIM(SUBSTITUTE(CLEAN(A2),CHAR(160)," ")). - To find the culprit, compare
=LEN(A2)with=LEN(TRIM(A2)), then check=UNICODE(RIGHT(A2,1))to get the code of the last character. - CLEAN does not touch characters above code 31, so a zero-width space (8203) needs its own
SUBSTITUTE(A2,UNICHAR(8203),""). - A VLOOKUP or XLOOKUP that returns #N/A on values that look identical is usually a trailing space.
=XLOOKUP(TRIM(A2),TRIM(D2:D500),E2:E500)fixes it without changing the source data.
The short answer
The Excel formula that removes hidden characters is =TRIM(CLEAN(A2)), and when that leaves something behind, use =TRIM(SUBSTITUTE(CLEAN(A2),CHAR(160)," ")). The second version exists because TRIM only knows about one kind of space. Text pasted from a website, a PDF, an email or an ERP export often carries a non-breaking space instead, which looks exactly like a space on screen but has a different character code, so TRIM walks straight past it.
Put the formula in a helper column, fill it down, then copy the column and paste it back as values over the original if you want the clean text to stay.
What TRIM and CLEAN actually remove
Most "TRIM not working" questions come from expecting either function to do more than it does. Microsoft's own documentation is precise about the limits.
TRIM removes spaces from the start and end of the text and shrinks any run of spaces between words down to a single space. It was built for the 7-bit ASCII space, character 32. It does not remove the non-breaking space, character 160, which is common in web content.
CLEAN removes the first 32 non-printing characters in the 7-bit ASCII code, codes 0 to 31. That covers tabs (9), line feeds (10) and carriage returns (13). It does not remove spaces of any kind, and Microsoft notes that it also leaves the extra non-printing characters 127, 129, 141, 143, 144 and 157 alone.
So the two functions cover different ground, and neither covers code 160 or anything in the wider Unicode range. That gap is where nearly every stubborn cell lives.
How to find the invisible character
Before cleaning anything, it helps to know what you are dealing with. Three small formulas do the job.
1. Count the characters. Put =LEN(A2) next to =LEN(TRIM(A2)). If the first number is bigger, there are extra normal spaces. If both numbers are the same but a lookup still fails, the extra character is something TRIM cannot see. Compare =LEN(A2) with the number of characters you can actually count: any difference is hidden.
2. Read the first and last character's code. Hidden characters usually sit at the ends. =UNICODE(RIGHT(A2,1)) returns the code of the last character and =UNICODE(LEFT(A2,1)) the first. The older =CODE() works the same way for codes up to 255 on Windows, but it cannot report characters beyond that range properly, so UNICODE is the safer habit.
3. Look at every character if you have to. In Excel 365 or Excel 2021, =UNICODE(MID(A2,SEQUENCE(LEN(A2)),1)) spills one code per character down the column. Anything that is not a letter, digit or 32 is your suspect.
Once you have the code, the table below tells you where it probably came from and how to remove it.
The hidden characters you will actually meet
| Code | Name | Where it usually comes from | Fix |
|---|---|---|---|
| 32 | Normal space (extra) | Typing, fixed-width exports, padded database fields | TRIM(A2) |
| 160 | Non-breaking space | Web pages, HTML emails, PDFs, some ERP and SAP exports | SUBSTITUTE(A2,CHAR(160)," ") then TRIM |
| 10 | Line feed (line break) | Alt+Enter inside a cell, multi-line form fields, CSV exports | SUBSTITUTE(A2,CHAR(10)," ") or CLEAN |
| 13 | Carriage return | Files made on Windows systems, usually paired with 10 | CLEAN(A2) |
| 9 | Tab | Text copied from tab-separated files or code editors | CLEAN(A2) |
| 8203 | Zero-width space | Web pages, chat apps, some content management systems | SUBSTITUTE(A2,UNICHAR(8203),"") |
| 65279 | Byte order mark | The first cell of some UTF-8 CSV files | SUBSTITUTE(A2,UNICHAR(65279),"") |
| 127 | Delete character | Old systems and terminal output | SUBSTITUTE(A2,CHAR(127),"") |
CHAR works with codes from 1 to 255 and uses your computer's character set, which on a Mac is not the same as on Windows. UNICHAR(160) gives the non-breaking space on both, so if your file moves between Mac and Windows, use UNICHAR in place of CHAR.
One formula for the usual mess
When a column has come from several places and you do not want to investigate each cell, stack the fixes. Work from the inside out: swap the characters TRIM cannot see for normal spaces, strip the control characters, then trim.
=TRIM(CLEAN(SUBSTITUTE(SUBSTITUTE(A2,UNICHAR(160)," "),UNICHAR(8203),"")))
Non-breaking spaces become normal spaces so TRIM can handle them. Zero-width spaces are deleted outright, because turning them into a normal space would split words that were never meant to be split. CLEAN then removes tabs and line breaks, and TRIM tidies the spacing.
One thing to watch: CLEAN removes a line break without putting anything in its place, so "Acme" on one line and "Ltd" on the next becomes "AcmeLtd". If your cells use Alt+Enter line breaks between words, replace them with a space first: =TRIM(SUBSTITUTE(A2,CHAR(10)," ")).
Numbers that still will not add up after cleaning
TRIM, CLEAN and SUBSTITUTE are text functions, so they always return text. Clean a column of amounts like " 1,250 " and you get "1,250" as text, which SUM ignores and which still sits on the left of the cell. Wrap the result in VALUE() or put two minus signs in front:
=VALUE(TRIM(SUBSTITUTE(A2,CHAR(160),""))) or =--TRIM(SUBSTITUTE(A2,CHAR(160),""))
For numbers, it is fine to replace the non-breaking space with nothing rather than a space, because some exports use it as a thousands separator. If VALUE returns #VALUE!, there is still a character left that is not part of a number, so go back to the UNICODE check above. The same hidden-text problem is one of the common reasons a SUMIFS returns zero on data that looks fine.
Fixing VLOOKUP and XLOOKUP #N/A caused by hidden spaces
A lookup that fails on two values you can see are identical is the most common way people discover hidden characters. "INV-1042" and "INV-1042 " are different values to Excel, and the trailing space is invisible in the cell.
The quick fix is to clean the lookup value inside the formula: =XLOOKUP(TRIM(A2),D2:D500,E2:E500). If the dirty side is the lookup column rather than the value, trim the range too: =XLOOKUP(TRIM(A2),TRIM(D2:D500),E2:E500). In Excel 365 and Excel 2021, TRIM over a range returns a cleaned array without needing Ctrl+Shift+Enter. For a non-breaking space, swap TRIM for the SUBSTITUTE version from earlier.
Trimming inside the lookup works, but it recalculates every time and hides the problem. If you use the column often, clean it once with a helper column and paste the result back as values. Our guide to XLOOKUP vs VLOOKUP covers the other reasons a lookup returns #N/A, such as numbers stored as text on one side only.
Removing hidden characters without a formula
Find and Replace. Select the column and press Ctrl+H. To remove line breaks, click in the Find what box and press Ctrl+J. Nothing visible appears, but Excel now searches for the line feed character. Put a space in Replace with and click Replace All. To target non-breaking spaces on Windows, click in Find what, hold Alt and type 0160 on the number pad, then replace with a normal space or nothing. This changes the cells directly, so work on a copy if you are unsure.
Power Query. If the same messy export arrives every week, clean it once in Power Query and refresh. Select the column, then Transform, Format, and choose Clean and then Trim. Be aware that Power Query's Trim is not the same as the worksheet TRIM: it removes spaces at the start and end of the text but leaves double spaces between words exactly as they are. To collapse internal spaces or remove a specific character such as the zero-width space, add a Replace Values step for it. Power Query's Clean removes control characters, which is close to what the worksheet CLEAN does.
If the column also has repeated rows once the text is clean, it is a good moment to remove duplicates, because entries that differed only by a hidden space now match each other.
Where hidden characters come from, and how to stop them
Hidden characters are rarely typed by hand. They arrive with data copied from somewhere else. Web pages use non-breaking spaces to keep words together on a line. PDFs often store spacing in ways that turn into odd characters when copied. Business systems pad fields to a fixed width with trailing spaces, which is why reports pulled out of ERP tools so often need trimming. Our guide to exporting SAP data to Excel covers the export settings that cause less of this in the first place.
Two habits help. When pasting from a browser or PDF, use Paste Special and choose Text or Values so formatting is left behind, though this will not strip the characters themselves. And when the same source feeds a sheet regularly, put the cleaning in one place, either a Power Query step or a single helper column, rather than fixing cells by hand each time. For a wider pass over a messy file, see how to clean messy data with AI.
Not sure which formula your data needs?
Describe the problem in plain English, like "remove the spaces that TRIM leaves behind in column A", and XLsheetAI writes the formula, explains each part, and lets you practise it on sample data.
Related reading: XLOOKUP vs VLOOKUP for lookups that still return #N/A after cleaning, SUMIFS not working for totals that come out as zero, and cleaning messy data with AI for the rest of the tidy-up.
FAQ
Which Excel formula removes hidden characters?
Start with =TRIM(CLEAN(A2)). CLEAN removes the non-printing control characters with codes 0 to 31, such as line breaks and tabs, and TRIM removes extra spaces. If the text came from a web page, a PDF or a system export, use =TRIM(SUBSTITUTE(CLEAN(A2),CHAR(160)," ")) so non-breaking spaces are turned into normal spaces first.
Why is TRIM not working in Excel?
Usually because the space is not a normal space. TRIM only removes the standard space character, code 32. Text copied from web pages and many PDFs often contains non-breaking spaces, code 160, which look identical but TRIM leaves them in place. Replace them with SUBSTITUTE(A2,CHAR(160)," ") before trimming.
What does the CLEAN function do in Excel?
CLEAN removes the first 32 non-printing characters in the 7-bit ASCII code, codes 0 to 31. That covers tabs, line feeds and carriage returns. It does not remove spaces, non-breaking spaces, or other Unicode characters such as the zero-width space.
How do I see which hidden character is in a cell?
Compare =LEN(A2) with =LEN(TRIM(A2)) to see whether extra characters exist. Then use =UNICODE(RIGHT(A2,1)) or =UNICODE(LEFT(A2,1)) to get the code of the last or first character. 160 is a non-breaking space, 10 is a line break, 9 is a tab and 8203 is a zero-width space.
Why does VLOOKUP return #N/A when the values look the same?
One side usually has a hidden character, most often a trailing space or a non-breaking space, or one value is a number and the other is a number stored as text. Clean the lookup value with TRIM inside the formula, for example =XLOOKUP(TRIM(A2),TRIM(D2:D500),E2:E500), or clean both columns once with a helper column.
