How to Remove Spaces and Hidden Characters Using TRIM and CLEAN in Excel

When you copy and paste data from a poorly formatted website or a legacy PDF document into Microsoft Excel, the text is often ruined by invisible formatting. You might see a client’s name with five accidental spaces before their first name, or weird, unprintable square symbols caused by broken line breaks. Attempting to write lookup formulas against this messy data will result in constant errors. To instantly sanitize text by stripping away illegal spaces and hidden characters, you must use the TRIM and CLEAN functions.

How the TRIM Function Works

The TRIM function is designed specifically to fix human typing errors related to the spacebar. It analyzes a string of text and deletes all leading spaces (spaces at the very beginning of the word) and all trailing spaces (accidental spaces at the very end). Crucially, if there are multiple spaces between two words, it deletes the extras, leaving exactly one single, standard space.

The syntax is: =TRIM(text)

If cell A2 contains the messy string    John      Smith    , click into cell B2 and type:

=TRIM(A2)

Excel instantly strips away all the invisible garbage spaces at the edges and perfectly compresses the middle, outputting exactly: John Smith.

How the CLEAN Function Works

While TRIM handles standard spacebar characters, it cannot handle deeper ASCII formatting issues. If you import data from an old enterprise database, it might contain “non-printable characters” (like carriage returns or system bell codes). These usually render in Excel as bizarre, hollow square boxes that completely break your formulas.

The CLEAN function specifically hunts down the first 32 characters of the 7-bit ASCII code (the non-printable system codes) and violently deletes them from the string.

The syntax is identical: =CLEAN(text)

Nesting TRIM and CLEAN Together

Because imported web data is almost always plagued by both extra spaces and illegal non-printable characters simultaneously, professional accountants rarely use these functions in isolation. Instead, you should nest them together into a single, unified sanitization formula.

If cell A2 is filled with an absolute disaster of imported web text, click into B2 and type:

=TRIM(CLEAN(A2))

Excel will calculate from the inside out. First, the CLEAN function rips out all the illegal system boxes and carriage returns. Then, it hands that resulting text over to the TRIM function, which perfectly smooths out all the accidental spaces. The final output is perfectly clean, standardized text that is guaranteed to work flawlessly with your lookup formulas.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.