TRIM Function

Excel Functions › Text

All Excel versions Text

The Excel TRIM function strips the spaces nobody wants: every leading space, every trailing space, and every run of spaces between words is collapsed to a single space. It is the first thing to reach for when a lookup mysteriously can’t find a value that is plainly right there — an invisible trailing space is the usual culprit. One important catch: TRIM does not remove the non-breaking space that web pages paste in, and this page shows the fix.


Quick answer: to clean the extra spaces out of A2:
=TRIM(A2)
"  Maria   Lopez " becomes "Maria Lopez" — ends stripped, the inside run collapsed to one space. For text pasted from the web, use =TRIM(SUBSTITUTE(A2, CHAR(160), " ")).

Syntax

=TRIM(text)
ArgumentDescription
textRequiredThe text (or cell reference) to clean. Leading and trailing spaces are removed; internal runs of spaces are collapsed to a single space.

What counts as a space? Only the regular space character — ASCII 32. The non-breaking space (CHAR(160)) that HTML pages use is a different character and sails straight through TRIM. Tabs and line breaks aren’t spaces either; those are CLEAN’s department.

Strip the extra spaces

Names exported from an old system arrive padded and double-spaced. One TRIM per cell normalizes all of it — and the LEN column proves characters really disappeared:

ABC
1Pasted name=TRIM(A2)=LEN(A2) → LEN(B2)
2  Maria   Lopez Maria Lopez14 → 11
3James  ChenJames Chen11 → 10
4 Priya Patel   Priya Patel15 → 11
5Dan    BrooksDan Brooks13 → 10
=TRIM(A2) // ends stripped, inner runs collapsed to one space

Note what TRIM keeps: exactly one space between words. To remove every space, that’s SUBSTITUTE:

=SUBSTITUTE(A2, " ", "") // removes ALL spaces, even between words

Try it: interactive TRIM demo

Live demo

Paste or type messy text — spaces show as · and non-breaking spaces as ° so you can see exactly what TRIM removes (and what it misses).

Fix broken lookups — and the CHAR(160) web-paste trap

The lookup repair. When =XLOOKUP("ACME", A:A, B:B) returns #N/A but you can see ACME in the column, a trailing space is almost always hiding in the data. Trim the lookup array on the fly:

=XLOOKUP("ACME", TRIM(A2:A100), B2:B100) // matches despite stray spaces in column A

Or fix the data itself with a helper column of =TRIM(A2), then paste-special as values over the original.

The CHAR(160) trap. Text copied from web pages often contains non-breaking spaces — character 160, not 32. They look identical, but TRIM ignores them. The bulletproof cleaner substitutes them into regular spaces first, then trims:

=TRIM(SUBSTITUTE(A2, CHAR(160), " ")) // the standard web-paste fix

Add CLEAN for imports that also carry line breaks and control characters — the full scrubber:

=TRIM(CLEAN(SUBSTITUTE(A2, CHAR(160), " "))) // handles spaces, nbsp, and nonprintables

How do you know spaces are the problem? Compare lengths — if these differ, invisible characters exist:

=LEN(A2) - LEN(TRIM(A2)) // 0 means no excess spaces

Errors & common pitfalls

Pitfall: TRIM doesn’t touch CHAR(160). The single most common “TRIM isn’t working” report. Non-breaking spaces from web pastes look like spaces but aren’t ASCII 32. Fix: =TRIM(SUBSTITUTE(A2,CHAR(160)," ")).

Pitfall: internal spaces are collapsed too. TRIM is not “trim the ends” like in most programming languages — it also squeezes every internal run down to one space. If your data legitimately contains double spaces, TRIM will eat them.

Pitfall: numbers come back as text. =TRIM(A2) on the number 42 returns the text “42”, which SUM ignores. Convert back with --TRIM(A2) or VALUE(TRIM(A2)) when trimming numeric columns.

Pitfall: tabs and line breaks survive. TRIM only knows the space character. Tabs (CHAR(9)) and line breaks (CHAR(10)) from imports need CLEAN: =TRIM(CLEAN(A2)).

Pitfall: TRIM doesn’t edit the cell. Like all formulas it returns a cleaned copy. To repair the data in place, put TRIM in a helper column, copy it, and Paste Special › Values over the original column.

Practice workbook

📊
Download the free TRIM practice workbook
Every example on this page, ready to open in Excel — plus practice challenges with answers on a separate tab. No sign-up required.

Frequently asked questions

Why is TRIM not removing spaces from my web-pasted data?
Because they aren’t spaces — they’re non-breaking spaces, character 160, which web pages use constantly. TRIM only removes regular spaces (character 32). Convert them first: =TRIM(SUBSTITUTE(A2,CHAR(160)," ")).
Does TRIM remove spaces between words?
Partially - it collapses every internal run of spaces down to exactly one. "Dan    Brooks" becomes "Dan Brooks", not "DanBrooks". To delete every space including single ones, use =SUBSTITUTE(A2," ","").
What's the difference between TRIM and CLEAN?
TRIM handles spaces; CLEAN handles nonprintable characters (codes 0–31, like tabs and line breaks). Imported data often needs both: =TRIM(CLEAN(A2)).
Why does my VLOOKUP fail even though the value is clearly there?
Almost always an invisible trailing space on one side of the match. Test with =LEN(A2)-LEN(TRIM(A2)) - anything above 0 means excess spaces. Then trim the lookup array in the formula or clean the column with a TRIM helper column.
How do I TRIM a whole column at once?
In Excel 365, give TRIM the whole range and it spills: =TRIM(A2:A100). In older versions, enter =TRIM(A2) in a helper column and fill down. Either way, finish with Copy then Paste Special › Values if you want to replace the original data.

Master functions like this in one day

This page covers one function. Our Excel Formulas and Functions class covers the 30 that matter most — live, hands-on, taught by professionals in Dallas–Fort Worth, Houston, Austin, Oklahoma City, Denver, or online.

See the Formulas & Functions Class

Related functions: CLEAN · SUBSTITUTE · LEN · CHAR · XLOOKUP