How to remove extra spaces in Google Sheets
Extra spaces are invisible, but they break things: "Acme " and "Acme" don't match in VLOOKUP, don't count as duplicates and sort in odd places. Here is how to find them and get rid of them.
Step 1: the quick fix with TRIM
=TRIM(A2) removes spaces at the start and end of the text and collapses any run of spaces inside it to a single one. " Juan Perez " becomes "Juan Perez". For a whole column in one go:=ARRAYFORMULA(IF(A2:A="","",TRIM(A2:A)))
Step 2: non-breaking spaces (CHAR(160))
Text copied from websites, PDFs or emails often carries non-breaking spaces. They look exactly like normal spaces but are a different character, so they tend to survive TRIM. Turn them into regular spaces first, then trim:=TRIM(SUBSTITUTE(A2,CHAR(160)," "))
If the data also has tabs or line breaks, REGEXREPLACE handles every kind of whitespace at once. \s covers spaces, tabs and line breaks, and \x{00A0} adds the non-breaking space:=TRIM(REGEXREPLACE(A2,"[\s\x{00A0}]+"," "))
REGEXREPLACE only works on text. If the column has numbers too, use A2&"" instead of A2.
Step 3: remove every space
For values that should never contain spaces, such as phone numbers, ID codes or SKUs, remove them all:=SUBSTITUTE(SUBSTITUTE(A2," ",""),CHAR(160),"")
Step 4: without formulas
Select the range and use Data → Data cleanup → Trim whitespace. It cleans leading, trailing and repeated spaces in place, with no helper column. If some cells still don't match after that, check them for non-breaking spaces with the formulas below and use step 2.
Step 5: check the result with LEN
Compare the length before and after cleaning:
=LEN(A2)-LEN(TRIM(A2))tells you how many extra regular spaces the cell has. Anything above 0 needs cleaning.=LEN(A2)-LEN(SUBSTITUTE(A2,CHAR(160),""))counts the non-breaking spaces.=CODE(LEFT(A2))shows the code of the first character. 32 is a normal space, 160 is a non-breaking one.
Once the counts are 0, copy the clean column and paste it over the original with Edit → Paste special → Values only. To fix capitalization at the same time, see how to fix name capitalization.
DataTools AI