How-to recipes · Cleaning data

How to remove spaces in Excel

Remove extra, leading, trailing or all spaces with TRIM and SUBSTITUTE, deal with non-breaking spaces from the web, and clean a column with Find & Replace.

Beginner TRIM SUBSTITUTE CHAR LEN CLEAN UNICHAR

Spaces you cannot see cause problems you can: a lookup returns #N/A although the name is clearly in the list, two identical-looking customers appear twice in a PivotTable, a sort puts some names at the top. The cause is often a space at the end of a value, two spaces between words, or a special non-breaking space copied from a web page. This recipe shows how to find them and remove exactly the spaces you want gone.

The example data

Three messy values in A2:A4. Column B shows their length with =LEN(A2), which is the quickest way to detect hidden spaces: Ana Silva should be 9 characters, but the cell holds 14.

Remove extra spaces with TRIM

TRIM removes spaces at the start and end of a text and reduces any run of spaces between words to a single space:

C2fx=TRIM(A2)
ABC
1Raw textLength
2 Ana Silva 14Ana Silva
3+30 210 555 019916
4Order Total 12

The result has 9 characters: one space between the names, none around them.

Result: Ana Silva

TRIM works the same in every version. In Microsoft 365 you can clean the whole column with one formula, =TRIM(A2:A100), which spills the cleaned list. In older versions put =TRIM(A2) in a helper column and copy it down.

Note. Do not confuse TRIM with TRIMRANGE, a newer Microsoft 365 function that trims empty rows and columns from the edges of a range. It does not touch spaces inside text.

Remove all spaces with SUBSTITUTE

Phone numbers, IBANs and product codes often need no spaces at all. SUBSTITUTE replaces every space with nothing:

C3fx=SUBSTITUTE(A3," ","")
ABC
1Raw textLength
2 Ana Silva 14
3+30 210 555 019916+302105550199
4Order Total 12

Every space is removed, including those between the digit groups.

Result: +302105550199

Non-breaking spaces from web pages

Text copied from websites and some PDFs contains the non-breaking space, character 160, instead of the normal space (character 32). It looks the same but TRIM ignores it, so the value stays dirty. Replace it with a normal space first, then TRIM:

C4fx=TRIM(SUBSTITUTE(A4,CHAR(160)," "))
ABC
1Raw textLength
2 Ana Silva 14
3+30 210 555 019916
4Order Total 12Order Total

A4 contains a non-breaking space between the words and a normal space at the end. The cleaned text has 11 characters.

Result: Order Total

To find out which character a stubborn space really is, use =CODE(MID(A4,6,1)) on its position: 32 is a normal space, 160 a non-breaking one. On Mac, use UNICHAR(160) instead of CHAR(160) because CHAR there uses a different character set.

Clean the cells in place

Formulas produce a cleaned copy. To replace the original values:

  1. Add a helper column with the TRIM formula and copy it down.
  2. Select the helper column, press Ctrl+C, select the original column and choose Home > Paste > Values.
  3. Delete the helper column.

For removing all spaces you can skip formulas entirely: select the column, press Ctrl+H, type one space in Find what, leave Replace with empty, and click Replace All. To catch non-breaking spaces with Find & Replace, click in Find what and type Alt+0160 on the numeric keypad.

Line breaks and other invisible characters

TRIM does not remove line breaks or tab characters. CLEAN removes non-printing characters such as line breaks (character 10), and the combination =TRIM(CLEAN(A2)) is a classic first cleaning step for imported data. Line breaks deserve their own recipe: How to remove line breaks in Excel.

Pitfalls

  • TRIM keeps single spaces between words, which is usually what you want for names but not for codes. Use SUBSTITUTE for codes.
  • Numbers become text. =TRIM(A2) on a number stored with spaces returns text. Convert with =--TRIM(A2) or VALUE(TRIM(A2)).
  • Formulas depend on the source. If you delete the original column after using TRIM, the formulas break. Paste them as values first.
  • Power Query has Transform > Format > Trim and Clean, which is the better choice for files you clean every week (Power Query basics: import, clean, append, merge, refresh).
Tip. After cleaning, lookups that returned #N/A usually start working. The full cleaning toolkit, including CLEAN, PROPER and Text to Columns, is in Text cleanup: TRIM, CLEAN, TEXTSPLIT, Flash Fill and Text to Columns and Removing duplicates and cleaning data.
⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 15 KB

More how-to recipes