How to use TRIM in Excel
TRIM removes spaces at the start and end of a text and reduces multiple spaces between words to one. It's the first step whenever data comes from another system and lookups mysteriously fail.
Syntax
=TRIM(text)
text | The text or cell. |
Examples
=TRIM(A2)" Sofia " → "Sofia".=VLOOKUP(TRIM(E2), A2:C100, 3, FALSE)Lookup that ignores stray spaces.=TRIM(SUBSTITUTE(A2, CHAR(160), " "))Also removes non-breaking spaces from web data.Clean a whole column with TRIM
TRIM doesn't change the original cells. Put the formula in a helper column and copy it down, then paste the results over the original as values (Ctrl+C, then Ctrl+Alt+V, V, Enter) and delete the helper column.
=TRIM(A2)A clean copy of A2: copy it down the whole column.Spaces TRIM can't remove
Text copied from websites often contains a non-breaking space (character 160). It looks like a normal space, but TRIM ignores it, so replace it first with SUBSTITUTE. Line breaks inside a cell go with CLEAN.
=TRIM(SUBSTITUTE(A2, CHAR(160), " "))Turns non-breaking spaces into normal ones, then trims.=TRIM(CLEAN(A2))Also removes line breaks and other invisible characters.Check whether a cell has extra spaces
Compare the length before and after trimming. Any difference means hidden spaces.
=LEN(A2)-LEN(TRIM(A2))How many extra spaces A2 has.=A2<>TRIM(A2)TRUE for the cells that need cleaning.Common mistakes
- Spaces are still there
- Text copied from websites often contains non-breaking spaces (CHAR(160)) that TRIM doesn't remove. Use the SUBSTITUTE version above.
Frequently asked questions
Why doesn't TRIM remove all the spaces?
TRIM removes normal spaces (character 32) only. Non-breaking spaces from web pages are character 160: replace them with SUBSTITUTE(A2, CHAR(160), " ") before trimming.
Does TRIM remove the spaces between words?
It keeps one space between words and removes the rest. To remove every space, use =SUBSTITUTE(A2, " ", "").
Can TRIM fix VLOOKUP #N/A errors?
Often, yes. A space at the end of a name or code is one of the most common reasons a lookup fails: =VLOOKUP(TRIM(E2), A2:C100, 3, FALSE).
Does TRIM work on numbers?
It does, but the result is text. Wrap it in VALUE to get a number back: =VALUE(TRIM(A2)).
Available in: All versions of Excel, Excel for the web, Google Sheets