Clean Up Messy Text

Excel Formulas › Text

All versionsTRIMCLEANSUBSTITUTE

Imported and pasted data is full of invisible junk — extra spaces, line breaks, non-breaking spaces — that breaks lookups and totals. TRIM, CLEAN, and SUBSTITUTE scrub it in one pass.


Quick formula: to remove extra spaces and line breaks from A2:
=TRIM(CLEAN(A2))
TRIM collapses runs of spaces to one (and strips leading/trailing); CLEAN removes non-printing characters like line breaks.

Functions used (tap for the full reference guide):

The example

Names with messy spacing. The cleaned column is reliable for lookups and matching.

AB
1RawCleaned
2  Maria  Lopez Maria Lopez
3Devon   SmithDevon Smith

The formula

The cleaned value in B2:

=TRIM(CLEAN(A2)) // " Maria Lopez " → "Maria Lopez"

How it works

Each function targets a different kind of junk:

  1. CLEAN(A2) strips non-printing control characters — the most common being the line breaks (CHAR(10)) that come from wrapped cells or web pastes.
  2. TRIM then removes leading and trailing spaces and collapses any run of internal spaces down to a single space.
  3. Together they turn " Maria Lopez " into a clean "Maria Lopez" that will match and look up correctly.

The sneaky one: non-breaking spaces. Text copied from the web often contains CHAR(160), which looks like a space but TRIM won’t touch. Convert it first: =TRIM(CLEAN(SUBSTITUTE(A2, CHAR(160), " "))).

Try it: interactive demo

Live demo

Type text with extra spaces; see what TRIM(CLEAN()) returns. (Length shows the junk being removed.)

Cleaned:   length

Variations

Remove ALL spaces (not just extras)

SUBSTITUTE deletes every space — handy for stripping spaces from phone numbers or codes:

=SUBSTITUTE(A2, " ", "")

Kill non-breaking spaces from web data

The full belt-and-suspenders clean:

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

Remove a specific character

Strip out, say, every comma:

=SUBSTITUTE(A2, ",", "")

Pitfalls & errors

Lookups still fail after TRIM. The culprit is almost always CHAR(160), the non-breaking space, which TRIM ignores. SUBSTITUTE it to a normal space first.

CLEAN misses some modern characters. CLEAN only removes the first 32 control characters. Stubborn invisibles (zero-width spaces, CHAR(160)) need SUBSTITUTE.

Convert formulas to values before deleting the original. Copy the cleaned column, Paste Special → Values, then you can remove the raw column safely.

Practice workbook

📊
Download the free Clean Up Messy Text practice workbook
Messy text with live TRIM/CLEAN, the non-breaking-space fix, and remove-all-spaces, plus 4 challenges with answers. No sign-up required.

Frequently asked questions

How do I remove extra spaces in Excel?
Use =TRIM(A2) to strip leading and trailing spaces and collapse multiple internal spaces to one. Wrap it in CLEAN to also remove line breaks: =TRIM(CLEAN(A2)).
Why does TRIM not remove all spaces?
TRIM ignores non-breaking spaces (CHAR(160)), which are common in web-copied text. Convert them first: =TRIM(CLEAN(SUBSTITUTE(A2, CHAR(160), " "))).
How do I remove all spaces, not just extra ones?
Use SUBSTITUTE to delete every space: =SUBSTITUTE(A2, " ", ""). Useful for codes and phone numbers where no spaces should remain.

Stop fighting formulas. Learn them in a day.

This recipe is one of hundreds of real-world formulas we teach. Our Excel Formulas & Functions class covers lookups, logic, text, and dynamic arrays hands-on — live in Dallas–Fort Worth, Houston, Austin, Oklahoma City, Denver, or online.

See the Formulas & Functions Class

Related formulas: Split text into columns · Extract first & last name · Join text with a delimiter

Function references: TRIM · CLEAN · SUBSTITUTE