How to Use the CLEAN Function in Excel – 10 Examples

This is an overview.

Excel CLEAN Function


CLEAN Function in Excel

Function Objective

The CLEAN function removes all non-printable characters from a text.

Syntax

=CLEAN(text)

Arguments

Argument Required/Optional Explanation
text Required The text string from which you want to remove all non-printable characters

Return Value

The text string with no non-printable characters.

Note

The CLEAN function can only remove non-printable characters represented by numbers 0 to 31 in the 7-bit ASCII code.


This is the sample dataset.

Excel CLEAN Function


Example 1 – Using CLEAN Function in Excel to Remove Non-printable Characters

The dataset showcases Email Ids with non-printable characters: CHAR(15), CHAR(12).

Excel CLEAN Function

  • Select the output cell D5.
  • Enter the following formula.
=CLEAN(C5)

C5 is the Imported Email Id from which you want to remove the non-printable characters.

removing non-printable characters

  • Press ENTER

removing non-printable characters

  • Drag down the Fill Handle to see the result in the rest of the cells.

removing non-printable characters

This is the output.

Excel CLEAN Function


Example 2 –Removing a Line Break with the CLEAN Function in Excel

 

Excel CLEAN Function

  • Select the output cell D5.
  • Enter the following formula.
=CLEAN(C5)

C5 is the Student Name from which you want to remove the line break.

removing line break

  • Press ENTER
  • Drag down the Fill Handle to see the result in the rest of the cells.

removing line break

This is the output.

removing line break


Example 3 – Using the CLEAN and the TRIM Functions to Remove Extra Spaces in Excel

Remove CHAR(15), CHAR(12) and CHAR(32) in the Imported Email Id column.

Excel CLEAN Function

  • Select the output cell D5.
  • Enter the following formula.
=TRIM(CLEAN(C5))

C5 is the Imported Email Id from which you want to remove the non-printable characters.
TRIM will remove all extra spaces from the text string.

removing extra space

  • Press ENTER
  • Drag down the Fill Handle to see the result in the rest of the cells.

removing extra space

This is the output.

removing extra space


Example 4 – Combining the CLEAN and the SUBSTITUTE Functions to Remove Non-printable Characters

In the Imported Email Id column, there are non-breaking spaces: CHAR(160).

Excel CLEAN Function

  • Select the output cell D5.
  • Enter the following formula.
=TRIM(CLEAN(SUBSTITUTE(C5,CHAR(160),"")))

C5 is the Imported Email Id from which you want to remove the non-printable characters.

SUBSTITUTE will replace CHAR(160) with a Blank, CLEAN will remove the non-printable characters CHAR(15), CHAR(12) and TRIM will remove all extra spaces from the text string.

SUBSTITUTE function

  • Press ENTER
  • Drag down the Fill Handle to see the result in the rest of the cells.

SUBSTITUTE function

This is the output.

Excel CLEAN Function


Example 5 – Using the Excel CLEAN and LEN Functions to Count the Number of Characters

Count the number of characters in Student Names:

number of characters

  • Select the output cell D5.
  • Enter the following formula.
=LEN(TRIM(CLEAN(C5)))

C5 is the Student Name whose character length you want to count.

CLEAN will remove the line break and TRIM will remove all extra spaces from the text string. LEN will count the character length.

number of characters

  • Press ENTER
  • Drag down the Fill Handle to see the result in the rest of the cells.

number of characters

This is the output.

Excel CLEAN Function


Example 6 – Using the CLEAN and the LEFT Functions to Extract Text in Excel

To extract the name portion in the Imported Email Id:

Excel CLEAN Function

  • Select the output cell D5.
  • Enter the following formula.
=LEFT(CLEAN(C5),FIND("@",CLEAN(C5),1)-1)

C5 is the Imported Email Id.

CLEAN will remove the non-printable characters and FIND(“@”, CLEAN(C5),1) will return the position of “@” in the text string. FIND will return 6:  6-1=5 will be the number of characters in the LEFT function, which will extract the first five characters.

extracting characters

  • Press ENTER
  • Drag down the Fill Handle to see the result in the rest of the cells.

extracting characters

This is the output.

Excel CLEAN Function


Example 7 – Replacing Characters using the SUBSTITUTE and the CLEAN Functions in Excel

Replace “gmail” with “yahoo” in the Imported Email Id column.

Excel CLEAN Function

  • Select the output cell D5.
  • Enter the following formula.
=SUBSTITUTE(CLEAN(C5),"gmail","yahoo")

C5 is the Imported Email Id.
SUBSTITUTE will replace “gmail” with “yahoo”

replacing characters

  • Press ENTER
  • Drag down the Fill Handle to see the result in the rest of the cells.

replacing characters

This is the output.

replacing characters


Example 8 – Checking If the Texts Need to be Cleaned

Excel CLEAN Function

  • Select the output cell D5.
  • Enter the following formula.
=IF(CLEAN(C5)=C5,"Cleaned","Not Cleaned")

C5 is the Imported Email Id.

CLEAN(C5)=C5 is the logical test: the text string is equal to the text string, in which all non-printable characters were removed. When it is TRUE, IF will return “Cleaned”. Otherwise “Not Cleaned”

checking if cleaning needs

  • Press ENTER
  • Drag down the Fill Handle to see the result in the rest of the cells.

checking if cleaning needs

This is the output.

checking if cleaning needs


Example 9 – Getting the Average Values Using the CLEAN and the VALUE Functions

Find the Average Marks:

Excel CLEAN Function

  • Select the output cell D5.
  • Enter the following formula.
=VALUE(CLEAN(C5))

C5 is the Imported Marks.

CLEAN will remove unnecessary data (but convert it to text) and VALUE will convert the text strings into numbers.

averaging values

  • Press ENTER
  • Drag down the Fill Handle to see the result in the rest of the cells.

averaging values

You will get the marks in number format.

averaging values

To get the Average Marks, enter the following formula in D11.

=AVERAGE(D5:D10)

D5:D10 is the Marks range.

averaging values

73.83 is the Average Mark.

averaging values


Example 10 – Using the CLEAN Function in Excel VBA to Remove Non-printable Characters

Excel CLEAN Function

  • Go to the Developer Tab>>Visual Basic.

VBA code

In the Visual Basic Editor:

  • Go to Insert >> Module

VBA code

A Module will be created.

VBA code

  • Enter the following code
Sub cleanspace()

  Range("D5") = Application.WorksheetFunction.Clean(Range("C5"))
  Range("D6") = Application.WorksheetFunction.Clean(Range("C6"))
  Range("D7") = Application.WorksheetFunction.Clean(Range("C7"))
  Range("D8") = Application.WorksheetFunction.Clean(Range("C8"))
  Range("D9") = Application.WorksheetFunction.Clean(Range("C9"))
  Range("D10") = Application.WorksheetFunction.Clean(Range("C10"))

End Sub

Non-printable characters in C5:C10 will be removed and the output will be displayed in Column D.

VBA code

  • Press F5

This is the output.

Excel CLEAN Function


Things to Remember

  • This function only removes  0 to 31 7-bit ASCII code from the given text.
  • The CLEAN function can also remove non-printable characters which are not visible.
  • It can not remove the non-printable characters which are not present in the ASCII code.

Download Workbook


<< Go Back to Excel Functions | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Tanjima Hossain
Tanjima Hossain

TANJIMA HOSSAIN is a marine engineer who enjoys working with Excel and VBA programming. For her, programming is a handy, time-saving tool for managing data, files, and online tasks. She's skilled in Rhino3D, Maxsurf C++, MS Office, AutoCAD, and Excel & VBA, going beyond the basics. She holds a B.Sc. in Naval Architecture & Marine Engineering from BUET and is now a content developer. In this role, she creates tech-focused content centred around Excel and VBA. Apart from... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo