Menu

How to Remove Spaces in Excel: TRIM, CLEAN, SUBSTITUTE

=TRIM(A2) removes the spaces before and after the text in A2 and turns runs of spaces between words into one. SUBSTITUTE removes every space or the non-breaking spaces TRIM misses.

Every sheet on this page is live: change a number or a formula and it recalculates.

=TRIM(A2) removes every space before and after the text in A2 and reduces each run of spaces between words to a single space. " Ana Silva " becomes Ana Silva.

Remove extra spaces
B2
ABCD
1NameTRIMLength beforeLength after
2 Ana Silva Ana Silva149
3Ben Okafor Ben Okafor1310
4 Chen WuChen Wu107
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

The spaces are invisible in column A, which is why the length columns are there: LEN counts them. Extra spaces usually arrive with imported or pasted data, and they break lookups and comparisons without any visible sign.

Why extra spaces break formulas

To Excel, Ana with a trailing space and Ana are different texts. A comparison returns FALSE, COUNTIF does not count the cell, and a lookup returns #N/A:

A trailing space breaks the match
D3
ABCD
1NameScoreIs it Ana?Ana's score
2Ana 90FALSEnot found
3Ben85TRUE90
4Chen78
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

C2 is FALSE and D2 finds nothing (without its fourth argument XLOOKUP would show #N/A) because A2 holds Ana with a space. D3 trims the whole lookup column inside the formula and finds 90. That works in Microsoft 365 and Excel 2021; in older versions, clean the column first. Spaces are one of the most common causes of #N/A from a lookup.

Remove all spaces

TRIM always keeps one space between words. To remove every space, for example from a phone number or a product code, substitute the space with nothing:

TRIM vs removing every space
B2
ABC
1CodeTRIMNo spaces
2 AB 12 34 AB 12 34AB1234
3555 201 3344555 201 33445552013344
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.
Clean the customer names
B2
AB
1CustomerClean name
2 Eli Novak
3Fay Ruiz
4Gus Lee
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

Your turn: The names in column A have extra spaces. In B2, return the name in A2 with no spaces around it and single spaces between words. The formula fills down to B4.

TRIM not working: non-breaking spaces

Text copied from a web page or a PDF often contains non-breaking spaces, character 160, instead of normal spaces, character 32. They look identical, but TRIM only removes character 32, so in Excel nothing changes:

A2:             Ana Silva   (a non-breaking space between the names and one at the end)
=LEN(A2)        10
=LEN(TRIM(A2))  10, TRIM removed nothing

The fix is to turn each character 160 into a normal space with SUBSTITUTE first, and let TRIM handle the rest. The text in A2 below is built with CHAR(160) so you can see the character a web page pastes:

Fix non-breaking spaces
D2
ABCDE
1Pasted textLengthCode of 4th characterFixedFixed length
2Ana Silva 10160Ana Silva9
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

C2 is how you identify an odd character: CODE(MID(A2,4,1)) returns the code of the 4th character, 160 here. D2 has 9 characters, the 10 of A2 minus the trailing space.

CLEAN: remove line breaks and other hidden characters

CLEAN removes the non-printing characters with codes 0 to 31: line breaks (10), carriage returns (13) and tabs (9). It does not touch spaces, so the usual pair is =TRIM(CLEAN(A2)):

CLEAN, then TRIM
B2
ABC
1ImportedCLEANTRIM(CLEAN)
2 Ana Silva AnaSilva AnaSilva
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

CLEAN deletes the line break outright, so the two names run together as AnaSilva . When a line break separates words, replace it with a space instead, =TRIM(SUBSTITUTE(A2,CHAR(10)," ")); the line break page shows that case.

Replace the original column with trimmed text

TRIM writes its result in another cell. To fix the data itself:

  1. Put =TRIM(A2) in an empty column next to the data and fill it down.
  2. Copy that column (Ctrl+C, Cmd+C on a Mac).
  3. Select the original column and use Home > Paste > Paste Values, so the cells get text, not formulas.
  4. Delete the helper column.

Find and Replace can remove spaces without a formula, but it cannot tell leading spaces from the ones between words: replacing a space with nothing joins Ana Silva into AnaSilva. Replacing two spaces with one, repeated until Excel finds nothing to replace, is the manual version of TRIM's inner-space rule. A single space at the start or end stays.

Frequently Asked Questions

How do I remove spaces in Excel?

=TRIM(A2) removes leading and trailing spaces and leaves one space between words. To remove every space, including those between words, use =SUBSTITUTE(A2," ","").

Why is TRIM not removing spaces?

The spaces are probably non-breaking spaces (character 160), which text copied from web pages often contains. TRIM only removes the normal space, character 32. Convert them first: =TRIM(SUBSTITUTE(A2,CHAR(160)," ")).

What is the difference between TRIM and CLEAN?

TRIM removes extra spaces. CLEAN removes non-printing characters with codes 0 to 31, such as line breaks and tabs. =TRIM(CLEAN(A2)) does both.

How do I replace the original column with the trimmed text?

Write =TRIM(A2) in a spare column and fill it down, copy that column, select the original and use Home > Paste > Paste Values. Then delete the helper column.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED