Text Formulas

How to Remove Extra Spaces in an Excel Formula

Use TRIM to remove leading, trailing and doubled spaces in an Excel formula, plus the non-breaking spaces and line breaks that TRIM alone misses.

On this page
  1. Use TRIM for ordinary extra spaces
  2. Clean the cells, not just the formula
  3. Count the extra spaces
  4. When TRIM doesn’t remove the space
  5. Trim before converting to a number
  6. A one-time fix with Find and Replace

Use TRIM for ordinary extra spaces

=TRIM(A2) removes spaces at the start and end of the text, and reduces any run of spaces inside it to a single space. So “ Jane Smith “ becomes “Jane Smith”.

TRIM only deals with ordinary space characters. Other kinds of space need the steps further down.

You can also use TRIM inside another formula when the spacing in the source isn’t reliable. For example, to look up a value that might have stray spaces:

=VLOOKUP(TRIM(A2),D:E,2,FALSE)

TRIM only cleans the value it’s applied to. If the list you’re searching also has stray spaces, clean that list as well. Stray spaces are one common reason a lookup misses its match, and why does vlookup return the wrong value covers the other causes.

Clean the cells, not just the formula

A formula leaves the original cells as they are. To clean a column for good:

  1. Put =TRIM(A2) in B2 and fill it down the column.
  2. Copy column B.
  3. Right-click A2, choose Paste Special, then Values.
  4. Delete column B.

Count the extra spaces

=LEN(A2)-LEN(TRIM(A2)) returns how many characters TRIM would remove from the cell. A result of 0 means there’s nothing to clean.

When TRIM doesn’t remove the space

TRIM doesn’t remove every kind of space. A non-breaking space, which often comes from web pages, has a different code and stays put. Replace it with an ordinary space first:

=TRIM(SUBSTITUTE(A2,UNICHAR(160)," "))

UNICHAR(160) is the non-breaking space. Line breaks also survive TRIM. They need CLEAN, which is covered in remove non printing characters in excel.

Trim before converting to a number

TRIM returns text, even when the result looks like a number. If you need to add the values, convert them afterward. The steps are in how to convert text to number in excel.

A one-time fix with Find and Replace

To remove doubled spaces across a range without a formula, select the cells, press Ctrl+H, type two spaces in Find what and one space in Replace with, then click Replace All. Repeat until nothing changes, since a run of three spaces needs two passes. Find and Replace changes the values directly, so keep a copy of the original data first.

The complete guide to text formulas

Text Formulas in Excel Explained
Read the guide

Keep exploring

Guides from other topics

All topics →