Skip to content

CLEAN

Removes non-printing characters, such as line breaks and tabs, from text.

Syntax

=CLEAN()

Text

All versions of Excel.

Try it

Edit the formula in C2 or change the inputs. The sheet recalculates as you type.

C2
A
B
C
1
Pasted code
Owner
Cleaned
2
FX-1010
Finance
FX-1010
3
FX-1085
Planning
4

C2 =CLEAN(A2) returns FX-1010

How CLEAN works

CLEAN removes non-printing characters from text. These are control characters, such as line breaks and tabs, that come along when you paste from emails, web pages, PDFs or other systems. They are often invisible, or show as small boxes, and they break lookups and comparisons in the same way a stray space does.

What it removes

CLEAN deletes characters with codes 0 to 31. The two you will meet most are:

  • Line feed, character 10. A line break inside a cell, as produced by Alt+Enter.
  • Tab, character 9.

=CLEAN("FX-1010" & CHAR(10)) returns FX-1010, and the hidden line break is gone.

What it does not remove

CLEAN does not remove ordinary spaces, so use TRIM for those. It does not remove non-breaking spaces, character 160, either. Convert them with SUBSTITUTE: =SUBSTITUTE(A2,UNICHAR(160)," "). A few other non-printing characters exist outside the first 32 codes, such as character 127. CLEAN leaves them as they are.

CLEAN joins words across a line break

CLEAN deletes a line break, it doesn't replace it with a space. =CLEAN("Pallet" & CHAR(10) & "wrap") returns Palletwrap. When a line break separates words, replace it with a space instead: =SUBSTITUTE(A2,CHAR(10)," "). Then apply TRIM if the break was next to other spaces.

The usual combination

For text of unknown origin, a full clean runs all three steps:

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

Read it from the inside: non-breaking spaces become ordinary spaces, non-printing characters are removed, and extra spaces are trimmed.

Finding out whether a cell has hidden characters

Compare lengths. =LEN(A2)-LEN(CLEAN(A2)) returns the number of non-printing characters CLEAN removes. A result above 0 means the cell has some. =CODE(RIGHT(A2,1)) shows the code of the last character, which reveals what it is: 10 for a line break, 9 for a tab, 32 for a space.

CLEAN returns text

Like the other text functions, CLEAN returns text. A cleaned "1250" is not a number until you convert it with VALUE. Applying CLEAN to a real number turns it into text, so keep it to text columns.

When to use it

Use CLEAN on imported data before a lookup, a match or a duplicate check. Use it on text you are about to put in a CSV or a system that does not accept line breaks. Do not use it on text where the line breaks are wanted, such as an address block, or you will lose the formatting.

Arguments

text
The text to clean. Usually a cell such as A2. CLEAN removes the first 32 non-printing characters (character codes 0 to 31) and leaves everything else, including spaces.

Examples

Each example is a live sheet. Change the inputs or the formula and watch the result.

Example 1

Remove a hidden line break

C2 =CLEAN(A2)

Result: FX-1010

A2 ends with a line break that you can't see. CLEAN removes it.

C2
A
B
C
1
Pasted code
Owner
Cleaned
2
FX-1010
Finance
FX-1010
3
FX-1085
Planning
4

C2 =CLEAN(A2) returns FX-1010

Example 2

Count the hidden characters

C2 =LEN(A2)-LEN(CLEAN(A2))

Result: 2

A tab and a line break, two characters, are removed by CLEAN.

C2
A
B
C
1
Pasted text
Owner
Hidden characters
2
Pallet wrap
Ops
2
3
4

C2 =LEN(A2)-LEN(CLEAN(A2)) returns 2

Example 3

Replace a line break with a space instead

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

Result: Pallet wrap

CLEAN would give "Palletwrap" because it deletes the break. SUBSTITUTE swaps it for a space.

C2
A
B
C
1
Two-line cell
Owner
One line
2
Pallet wrap
Ops
Pallet wrap
3
4

C2 =SUBSTITUTE(A2,CHAR(10)," ") returns Pallet wrap

Example 4

A full clean: non-breaking spaces, control characters, extra spaces

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

Result: Floor cleaner

SUBSTITUTE fixes the non-breaking space, CLEAN removes the tab, and TRIM removes the leading space.

C2
A
B
C
1
Web import
Qty
Cleaned
2
Floor cleaner
24
Floor cleaner
3
4

C2 =TRIM(CLEAN(SUBSTITUTE(A2,UNICHAR(160)," "))) returns Floor cleaner

Your turn.

Solve it in the sheet. Every answer is checked, with hints if you need them.

Task 1 of 2

In B2, return the code in A2 without its hidden line break or tab. Fill it down to B5.

Answer in B2, then it fills down to B5.

B2
A
B
C
D
1
Pasted code
Cleaned
Hidden chars
Owner
2
FX-1010
Finance
3
FX-1085
Planning
4
FX-2040
Ops
5
FX-3012
Ops
6
7

Common mistakes

  • CLEAN deletes line breaks instead of replacing them. Words on either side join: "Pallet" and "wrap" become "Palletwrap". Replace the break with a space first, using SUBSTITUTE(A2,CHAR(10)," ").
  • CLEAN does not remove spaces. Pair it with TRIM for extra spaces, and use SUBSTITUTE for non-breaking spaces (character 160).
  • CLEAN removes only characters 0 to 31. Some other invisible characters, such as character 127, stay. If LEN still looks wrong after CLEAN, check the last character with CODE(RIGHT(A2,1)).
  • CLEAN returns text. A cleaned number such as "1250" won't be summed until you convert it with VALUE.
  • Don't run CLEAN on text where the line breaks are wanted, such as an address in one cell. It will flatten the layout.

Questions people ask

How do I remove line breaks in Excel?

To delete them, use `=CLEAN(A2)`. To replace them with a space, use `=SUBSTITUTE(A2,CHAR(10)," ")`, which stops the words on either side from running together.

What is the difference between CLEAN and TRIM?

CLEAN removes non-printing characters such as line breaks and tabs. TRIM removes extra spaces. For messy data, use `=TRIM(CLEAN(A2))`.

Why does CLEAN not remove a space that looks wrong?

The space may be a non-breaking space (character 160), which CLEAN and TRIM both leave alone. Use `=SUBSTITUTE(A2,UNICHAR(160)," ")` first.

How can I tell whether a cell has hidden characters?

Compare `=LEN(A2)` with `=LEN(CLEAN(A2))`. A difference means CLEAN found non-printing characters. `=CODE(RIGHT(A2,1))` shows what the last character is.

Does CLEAN work on numbers?

It accepts them, but it returns the number as text. Use it on text columns, and convert the result with VALUE if you need a number.

Learn CLEAN in a real sheet.

CLEAN is taught in Logic and text, lesson 7: TRIM and CLEAN. Each lesson is a short, graded exercise like the one above.