How to clean imported data in Excel

Imported text fails later formulas when the extra characters are still in the cell. TRIM, CLEAN, and SUBSTITUTE remove different characters. How to split first and last names in Excel already uses the padded Maria Lopez cell, and it says a nonbreaking space, character 160, survives TRIM.

Ordinary spaces collapse to one

TRIM of Maria Lopez is Maria Lopez. The leading spaces go away, the trailing spaces go away, and the two spaces between the names become one.

TRIM
=TRIM("  Maria  Lopez  ")
=TRIM(A2)B2
Ordinary spaces
AB
1RawClean
2 Maria Lopez Maria Lopez
A picture of the TRIM page. Two spaces before Maria, two between the names, and two after Lopez become one space between Maria and Lopez.
=TRIM("  Maria  Lopez  ")

That is the name the split page starts from after TRIM. TEXTBEFORE of the raw cell, before TRIM, is blank, because the first character is a space. Clean the cell before you split it.

TRIM does not remove character 160. A name that looks padded and does not change is often that character. SUBSTITUTE of CHAR(160) with an ordinary space, then TRIM, is the fix:

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

A line break becomes no space at all

CLEAN removes characters 0 through 31. A line break is character 10. Austin, a line break, and TX become AustinTX. CLEAN does not insert a space where the line break was.

CLEAN
=CLEAN("Austin"&CHAR(10)&"TX")
=CLEAN("Austin"&CHAR(10)&"TX")B2
A line break
AB
1RawClean
2Austin, then a line break, then TXAustinTX
A picture of the CLEAN page. Character 10 is the only thing between Austin and TX, so removing it leaves AustinTX with no space.
=CLEAN("Austin"&CHAR(10)&"TX")

CLEAN also leaves character 160 in place, and it leaves ordinary spaces in place. The CLEAN page says both. If the words should stay apart, the space has to be in the text you keep. SUBSTITUTE of the line break with a space does that. CLEAN will not.

Collapse space does the 160 and the TRIM

xl.collapse.space substitutes character 160 with an ordinary space, then TRIM. Two spaces, the word Austin, and two spaces become Austin.

xl.collapse.space
=xl.collapse.space("  Austin  ")
=xl.collapse.space(A2)B2
Padded Austin
AB
1RawClean
2 Austin Austin
A picture of the collapse-space page. Two spaces, Austin, and two spaces become Austin. The name also turns character 160 into an ordinary space before TRIM.
=xl.collapse.space("  Austin  ")
=TRIM(SUBSTITUTE("  Austin  ",CHAR(160)," "))

The name needs Microsoft 365, Excel 2024, or Excel for the web. Excel 2021 returns #NAME?. The TRIM of SUBSTITUTE above is the same two steps without the name. It does not run CLEAN. A line break still needs CLEAN, or a SUBSTITUTE of CHAR(10), before the spaces are collapsed.

The nested order on the CLEAN page is TRIM of a CLEAN of a SUBSTITUTE. SUBSTITUTE handles 160. CLEAN handles the line break. TRIM handles the ordinary spaces. Each one leaves the characters the others own.


More from the blog