TRIM removes extra spaces from text — the invisible characters that silently break lookups and counts. Here is everything you need to know.
TRIM removes extra spaces from text — spaces at the start, spaces at the end, and multiple consecutive spaces in the middle (colapsing them to a single space). It leaves exactly one space between words and nothing before or after.
This sounds minor but it is one of the most important data cleaning formulas in Excel. Extra spaces are invisible — you cannot see them — but they silently break VLOOKUP matches, COUNTIF counts, and text comparisons. A name with a trailing space looks identical to one without but will never match in a lookup.
=TRIM(text)| Argument | Description |
|---|---|
| text required | The text string or cell reference to remove extra spaces from. |
=TRIM(A2)
← " John Smith " → "John Smith"
=TRIM(A2)
← "John Smith" (2 spaces) → "John Smith" (1 space)
TRIM collapses all multiple spaces between words to a single space, not just leading and trailing ones.
If your lookup is failing for values that look identical, spaces are usually the cause.
=VLOOKUP(TRIM(A2), $C:$D, 2, 0)TRIM the lookup value before passing it to VLOOKUP. Also TRIM the lookup column itself if spaces are on that side.
=TRIM(UPPER(A2))Remove spaces AND standardise capitalisation in one formula. Essential when merging data from multiple sources where entries were typed inconsistently.
=COUNTIF(A2:A100, TRIM(E1))If E1 has extra spaces, the COUNTIF would miss matches. TRIM ensures the criteria is clean before counting.
Use LEN to detect hidden spaces — if LEN differs from the trimmed version, spaces are present.
=IF(LEN(A2) <> LEN(TRIM(A2)), "Has spaces", "Clean")
When you import data from external systems — CSV files, database exports, web scrapes, copied text — extra spaces almost always come along for the ride. A standard data cleaning routine looks like this:
Step 1: Remove spaces
=TRIM(A2)
Step 2: Standardise capitalisation
=PROPER(TRIM(A2))
Step 3: Remove line breaks (TRIM doesn't catch these)
=TRIM(SUBSTITUTE(A2, CHAR(10), " "))
TRIM only removes the standard space character (ASCII 32). It does not remove:
=SUBSTITUTE(A2, CHAR(160), "") to remove these.=SUBSTITUTE(A2, CHAR(10), " ") to replace line breaks with spaces, then TRIM.=SUBSTITUTE(A2, CHAR(9), "")Combine TRIM with SUBSTITUTE to handle all invisible characters: =TRIM(SUBSTITUTE(SUBSTITUTE(A2,CHAR(160)," "),CHAR(10)," "))
ExcelPro has TRIM, PROPER, SUBSTITUTE and UPPER exercises in every track. Real messy data, instant feedback, free to start.
Try cleaning exercises →