=TRIM(CLEAN(SUBSTITUTE(A2, CHAR(160), " "))) Use =TRIM(A2) to delete all leading and trailing spaces and reduce multiple consecutive spaces into a single space. For stubborn web-scraped non-breaking spaces (CHAR 160), use =TRIM(CLEAN(SUBSTITUTE(A2, CHAR(160), " "))).
How to Remove Leading, Trailing, and Extra Spaces in Excel
Hidden spaces are the #1 cause of broken VLOOKUP, XLOOKUP, and COUNTIF formulas. If cell A2 has "Apple " and your lookup is searching for "Apple", Excel will return #N/A.
1. The Standard TRIM Formula
=TRIM(A2)
- Removes all leading spaces at the start of the string.
- Removes all trailing spaces at the end of the string.
- Condenses internal double spaces down to single spaces.
2. The Ultimate Bulletproof Clean Formula (Web Imports)
When data is copied from web pages or ERP systems, it often contains Non-Breaking Spaces (CHAR(160)) and Non-Printable Line Breaks (CHAR(1) to CHAR(31)):
=TRIM(CLEAN(SUBSTITUTE(A2, CHAR(160), " ")))
Breakdown of the 3-Layer Cleanser:
SUBSTITUTE(A2, CHAR(160), " "): Converts invisible non-breaking spaces into normal standard spaces.CLEAN(...): Removes invisible ASCII control characters (carriage returns, tabs, null bytes).TRIM(...): Strips out the remaining leading, trailing, and excessive spaces.
Frequently Asked Questions
Why does TRIM not remove some spaces exported from web applications?
Web exports often use HTML non-breaking spaces ( or ASCII/Unicode 160). TRIM only removes standard space (ASCII 32). Replace CHAR(160) with a normal space first using SUBSTITUTE(A2, CHAR(160), " ").