Instant digital downloadExcel and Google SheetsSecure card checkout

Excel and Google Sheets how-to

Remove extra spaces in Excel and Google Sheets with TRIM

Names that will not sort right, a VLOOKUP that says #N/A for a code you can see in the list, two cells that look the same but do not match: a space you cannot see is often the cause. TRIM fixes most of them, and one more step fixes the rest.

ABCD
1Name as typedLENClean nameLEN
2 Ana Silva11Ana Silva9
3Ben Okafor 12Ben Okafor10
4 Cara Liu11Cara Liu8
5Dev Patel 11Dev Patel9
6Eli Novak9Eli Novak9

Sample data: names typed with stray spaces in A2:A6. B2 is =LEN(A2), C2 is =TRIM(A2) and D2 is =LEN(C2), each filled down. The names in A and C look alike on screen; the two LEN columns show the extra spaces.

TRIM in one formula

Say the names in column A came from a form or another system with stray spaces. In C2, type:

=TRIM(A2)

and fill it down. Each name comes back with no spaces at the start or the end, and with one space between words where there were two or three. Microsoft's TRIM page says it in one line: TRIM "Removes all spaces from text except for single spaces between words." Google Sheets has the same function, and Google's TRIM help page says it "Removes leading, trailing, and repeated spaces in text."

See the spaces you cannot see

A space at the end of a cell is invisible, so check with LEN, which counts every character, spaces included. In the sample, =LEN(A3) gives 12 for Ben Okafor and =LEN(C3) gives 10 after TRIM. To count just the extra spaces in one step:

=LEN(A2)-LEN(TRIM(A2))

Anything above 0 means the cell has spaces to clean. This is also why two cells that look the same can fail to match. With Ana Silva and a space after it in F2 and plain Ana Silva in F3, =F2=F3 says FALSE, and =TRIM(F2)=F3 says TRUE.

Replace the messy column with the clean one

TRIM gives you a second, clean column, and the original keeps its spaces. To swap them, select the TRIM results and copy them, then select the first cell of the original column and paste them as values: in Excel, Home > Paste > Paste Values; in Google Sheets, Edit > Paste special > Values only. Then delete the helper column.

Paste as values matters here. A plain paste would put the TRIM formulas themselves into column A, where each one would point at its own cell.

VLOOKUP or XLOOKUP says #N/A because of a space

An order code typed as B300 with a space after it does not match B300 in the price list, so =VLOOKUP(G2,D2:E4,2,FALSE) returns #N/A even though the code is right there. Clean the code inside the lookup:

=VLOOKUP(TRIM(G2),D2:E4,2,FALSE)

That found B300 and returned its price, 10, in our test. XLOOKUP works the same way: =XLOOKUP(TRIM(G2),D2:D4,E2:E4). If the spaces are in the price list instead, TRIM on the code cannot help, so clean the list itself as in the section above. For codes that really are missing, the IFERROR page shows how to show a message instead of #N/A.

The space TRIM misses: character 160

Text copied from a web page, an email or a PDF often carries a non-breaking space, a different character that looks exactly like a space. Microsoft's TRIM page says it has "a decimal value of 160" and that "By itself, the TRIM function does not remove this nonbreaking space character." Google's page says the same for Sheets: "Whitespace or non-breaking space will not be trimmed." So swap each one for a normal space first, then trim:

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

UNICHAR works in Excel 2013 and later and in Google Sheets. In Excel 2010 or older on Windows, use CHAR(160) in its place.

When TRIM has run, LEN looks right and a match still fails, this is the usual cause. A character 160 in place of a normal space does not change the length at all: Ana Silva has 9 characters either way.

Counting when some cells have spaces

COUNTIF compares whole cells, so North with a space after it is not North. In a region column where five cells say North but three of them carry a stray space, =COUNTIF(A2:A7,"North") counts 2. This counts all five:

=SUMPRODUCT(--(TRIM(A2:A7)="North"))

TRIM cleans each cell in the range before the comparison, and the two minus signs turn TRUE and FALSE into 1 and 0 for SUMPRODUCT to add. If you count that column often, cleaning it once as above is the better fix.

Cells that look empty but hold spaces

A cell with only a space or two in it looks blank, but =A8="" says FALSE. This is TRUE for empty cells and for cells of spaces alike:

=LEN(TRIM(A8))=0

The blank cells page puts it inside IF and has a practice tab for it.

Line breaks inside a cell

A cell where someone pressed Alt+Enter in Excel holds a line break, character 10, between the words. TRIM leaves it in place. To turn it into a space:

=TRIM(SUBSTITUTE(A2,CHAR(10)," "))

Microsoft's CLEAN page says CLEAN was designed to remove "the first 32 nonprinting characters in the 7-bit ASCII code (values 0 through 31)", which takes in line breaks. But it puts nothing in their place, so Ana and Silva on two lines came out as AnaSilva in our test. Use SUBSTITUTE when the break sits between two words.

Remove every space, not only the extras

For phone numbers, postal codes or product codes where no space belongs, replace each space with nothing:

=SUBSTITUTE(A2," ","")

604 555 0182 becomes 6045550182. If the text came from a web page, take out the character 160 too: =SUBSTITUTE(SUBSTITUTE(A2," ",""),UNICHAR(160),"").

Free practice sheet

Three jobs to try: clean a list of names and count how many extra spaces each one had; fix a price lookup that says #N/A for order codes typed with stray spaces; and clean names pasted from a web page, where plain TRIM still leaves four of five wrong. Yellow cells for your formulas, a Check column that says Correct or Try again, and an Answers tab. One Excel file that opens in Google Sheets too (Excel 2013 or later for the last tab). Free, no sign up. Sample data.

Download the practice sheet (.xlsx)

Templates with the math already done

Rather skip the formulas? These templates come with every formula built and checked, plus a filled-in sample.

Teaching this to a class or team?

Get one tip like this a month

A free email from VARS Digital with one Excel or Google Sheets fix, tested in both apps. No spam, unsubscribe any time.

By signing up you agree that VARS Digital, 10151 240 Street, Maple Ridge, BC V2W 0G9, Canada (inquiries@varsdigital.app) may email you its newsletter: Excel and Google Sheets tips, updates and news about our templates and lessons. We send a confirmation email first, and nothing else goes out until you click it. Every email has an unsubscribe link, and you can leave any time. Details in our store policies.

More how-tos