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:
- Put
=TRIM(A2)in B2 and fill it down the column. - Copy column B.
- Right-click A2, choose Paste Special, then Values.
- 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.