How to Remove Spaces in Excel (Leading, Trailing, Extra and All)
Leading and trailing spaces, and runs of spaces between words, are all cleaned up by TRIM alone. It leaves one space between words.
Formula to use
=TRIM(A2)Removes leading and trailing spaces; extra spaces between words become one
Remove every space
For values that must have no spaces at all, such as phone numbers or codes, replace spaces with empty text using SUBSTITUTE.
=SUBSTITUTE(A2, " ", "")Removes all spaces
Spaces TRIM will not remove
Data copied from web pages can contain non-breaking spaces (code 160) mixed in with normal spaces (code 32). TRIM removes only normal spaces, so change them to normal spaces first and then TRIM.
=TRIM(SUBSTITUTE(A2, CHAR(160), " "))Also cleans up spaces from the web
Check for spaces
Comparing lengths with LEN reveals hidden spaces. Check what the first character is with CODE.
=LEN(A2)-LEN(TRIM(A2))Number of spaces TRIM would remove=CODE(A2)32 means the first character is a normal space
Common mistakes
The result of TRIM is text. If you removed spaces around a number, convert it with VALUE before calculating with it.
To clean the original, copy the result and use Paste Values in the original place.
Try the functions used here
Change the arguments in the function playground and watch the calculation step by step.
- TRIM functionRemoves leading and trailing spaces and leaves single spaces between words.
- SUBSTITUTE functionReplaces specific text inside a text with other text.
- LEN functionCounts the characters in a text. A space counts as one character.
- CODE functionReturns the character code of the first character of a text.
- VALUE functionConverts text that looks like a number into a real number.
FAQ
- VLOOKUP returns #N/A, but the values look the same.
- There is often a hidden space on one side. Try wrapping the lookup value in TRIM(A2).
- How do I remove line breaks too?
- Remove them with SUBSTITUTE(A2, CHAR(10), "").