Text function

TRIM function

Removes spaces at the start and end of text and reduces any run of spaces between words to a single space.

Available in All versions of Excel, including Microsoft 365, Excel for Mac and Excel for the web

In every Excel versionWorks in Google Sheets

Syntax

TRIM(text)
ArgumentWhat it means
textRequiredThe text or cell to clean.

Returns: The text with extra spaces removed.

How TRIM works

Extra spaces are the most common reason why "identical" values do not match. Data typed by people, exported from other systems or pasted from emails often has a space at the end (Mouse ) or doubled spaces in the middle (Ana Silva). They are invisible, but VLOOKUP, XLOOKUP, COUNTIF and duplicate checks treat the values as different. TRIM fixes this: it removes all leading and trailing spaces and squeezes every run of spaces between words down to one.

TRIM only removes the normal space character (code 32). Web pages often use the non-breaking space (code 160), which TRIM ignores; replace it first with SUBSTITUTE(A2,CHAR(160)," "). Line breaks and other control characters need CLEAN. Together they form the standard cleaning formula =TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," "))).

TRIM returns a new value; to clean the original column, copy the TRIM results and Paste Values over it. In Microsoft 365 you can trim a whole range in one go: =TRIM(A2:A500) spills the cleaned list.

Examples

1Clean up a name

B2fx=TRIM(A2)
AB
1Name (imported)
2 Ana Silva Ana Silva

Result: Ana Silva

Leading and trailing spaces are removed and the three spaces between the names become one.

2Make a lookup work

E2fx=VLOOKUP(TRIM(D2),A2:B4,2,FALSE)
ABCDE
1ProductPriceLookupPrice found
2Keyboard25Mouse 12
3Mouse12
4Monitor180

Result: 12

D2 contains Mouse with a trailing space, so a plain VLOOKUP returns #N/A. TRIM removes the space and the match succeeds.

3Non-breaking spaces from a web page

B2fx=TRIM(SUBSTITUTE(A2,CHAR(160)," "))
AB
1Product (copied from web)
2Laptop  Stand Laptop Stand

Result: Laptop Stand

On its own TRIM would leave the text unchanged, because the spaces are code 160. SUBSTITUTE turns them into normal spaces first.

⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 15 KB

Tips

  • Check whether a cell has extra spaces with =LEN(A2)<>LEN(TRIM(A2)).
  • Use TRIM inside lookups rather than on the lookup table when you cannot change the source data.
  • Power Query's Transform > Format > Trim removes only leading and trailing spaces, not doubled inner spaces - TRIM in the sheet does both.

Questions

Why does TRIM not remove some spaces?

They are probably non-breaking spaces (code 160) from a web page. Use =TRIM(SUBSTITUTE(A2,CHAR(160)," ")).

Does TRIM remove single spaces between words?

No. It keeps one space between words and only removes extras plus any at the start and end.

How do I remove all spaces?

Use =SUBSTITUTE(A2," ",""), which deletes every space including those between words.

Google Sheets

TRIM also exists in Google Sheets and works the same way for everyday use, so the examples above work there too.

TRIM in other languages

In a non-English version of Excel the function has a local name. Excel translates formulas automatically when a file is opened in another language.

LanguageFunction name
CzechPROČISTIT
DanishFJERN.OVERFLØDIGE.BLANKE
DutchSPATIES.WISSEN
FinnishPOISTA.VÄLIT
FrenchSUPPRESPACE
GermanGLÄTTEN
HungarianKIMETSZ
ItalianANNULLA.SPAZI
Norwegian (Bokmål)TRIMME
PolishUSUŃ.ZBĘDNE.ODSTĘPY
Portuguese (Brazil)ARRUMAR
Portuguese (Portugal)COMPACTAR
RussianСЖПРОБЕЛЫ
SpanishESPACIOS
SwedishRENSA
TurkishKIRP

Recipes that use TRIM