What is the TRIM function?
=TRIM(text)TRIM removes all spaces at the start and end of a text and reduces runs of spaces between words to one. Use it to clean data pasted from other programs or the web.
When VLOOKUP or COUNTIF can't find a matching value, invisible spaces are often the cause. In the strip a dot (·) is a space, and playing the steps shows the removed spaces in red.
Arguments
- text
- The text or cell to clean up.
TRIM examples
Example 1. Clean up doubled spaces
=TRIM(" Excel functions guide ")Removes 2 leading, 1 trailing and the extra spaces between words: "Excel functions guide".
Example 2. Example from Microsoft
=TRIM(" First Quarter Earnings ")Removes the leading and trailing spaces: "First Quarter Earnings".
Example 3. Clean before a lookup
=VLOOKUP(TRIM(A2), table, 2, FALSE)Removes spaces from the lookup value first.
Common mistakes
Spaces copied from the web are not removed
According to Microsoft, TRIM removes only the normal space (value 32), not the non-breaking space (value 160) common on web pages. Replace it first with SUBSTITUTE(A2, CHAR(160), " ") and then use TRIM.
Expecting every space between words to go
TRIM leaves one space between words. To remove all spaces, use SUBSTITUTE(A2," ","").
FAQ
- Does it change the original data?
- No. The result appears in the formula cell. To replace the original, copy the result and paste as values.
- How do I count the spaces?
- =LEN(A2)-LEN(TRIM(A2)) counts the spaces TRIM would remove.