Excel formula: Remove leading and trailing spaces

When you import data from other devices, there may be some extra leading and trialing spaces in text strings in Excel cells. Now, you need to remove these leading and trailing spaces as below screenshot shown. Here, this tutorial provides some formulas to finish three operations in Excel:
Remove the extra spaces including leading and trailing spaces
Remove extra spaces and non-printing characters
Remove extra spaces and non-printing spaces and characters

Remove the extra spaces including leading and trailing spaces

Generic formula:

 TRIM(text)

Arguments

 Text: the text string or cell reference you want to remove leading and trailing spaces from.

How this formula work

Cell B3 contains the text string you want to remove the leading and trailing spaces from, select a cell which you want to place the result, then use below formula

 =TRIM(B3)

Press Enter key, the leading and trailing and other extra spaces are removed. If you need, drag fill handle over cells to apply this formula.

Explanation

The TRIM function is used to remove all extra spaces including leading and trailing spaces from text string.

Remove extra spaces and non-printing characters

If there are some non-printing characters you want to remove together with the leading and trailing spaces, you can use the formula which combines the TRIM and CLEAN functions.

Generic formula:

 TRIM(CLEAN(text))

Arguments

 Text: the cell reference or text string you want to use.

How this formula work

In cell B3:B5, there are the texts including leading and trailing spaces and some non-printing characters. Here in cell C3, use below formula to remove the extra spaces and non-printing characters (visible).

 =TRIM(CLEAN(B3))

Press Enter key to get the result, and drag fill handle over cells to fill this formula.

Explanation

The CLEAN function removes the characters which code values between CHAR(0) and CHAR(31) in 7-bit ASCII code from text string. CHAR(16)-CHAR(27) are the visible non-printing characters.

The TRIM function removes extra spaces.

If you want to remove both of visible and invisible non-printing characters and extra spaces, please go to next formula.

Remove extra spaces and non-printing spaces and characters

Sometimes, some spaces appeared in text string as CHAR(160), which the TRIM function cannot remove them. In this case, you need to combine the SUBSTITUTE, CLEAN and TRIM functions.

Generic formula:

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

Arguments

 Text: the text string you want to use.

How this formula work

Example: To remove non-printing characters and spaces from cell B3, select a cell, and use below formula:

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

Press Enter key, and then drag auto fill handle over cells to use this formula.

Explanation

CHAR(160): the non-printing space which has value 160 in 7-bit ASCII system.

SUBSTITUTE function: the SUBSTITUTE function replaces old text with the new text. Here the formula SUBSTITUTE(B3,CHAR(160)," ") replaces the non-printing spaces with regular spaces.

The CLEAN function removes the characters which code values between CHAR(0) and CHAR(31) in 7-bit ASCII code from text string. CHAR(16)-CHAR(27) are visible non-printing characters.

The TRIM function removes extra spaces.

Sample File

Relative Formulas

• Check if a cell contains a specific text
To check if a cell contains some texts in range A but does not contain the texts in range B, you can use an array formula which combines the COUNT, SEARCH and AND function in Excel
• Check if a cell contains one of several values but exclude other values
This tutorial will provide a formula to quickly handle the task that check if a cell contains one of things but excluding other values in Excel and explain the arguments of the formula.
• Check if cell contains one of things
Supposing in Excel, there is a list of values in column E, you want to check if the cells in column B contain all of the values in column E, and return TRUE or FALSE.
• Check if cell contains number
Sometimes, you may want to check if a cell contains numeric characters. This tutorial provides a formula which will return TRUE if the cell contains number, FALSE if cell does not contain number.

The Best Office Productivity Tools

Kutools for Excel - Helps You To Stand Out From Crowd

 ðŸ¤– Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functionsâ€¦ Popular Features: Find, Highlight or Identify Duplicates  |  Delete Blank Rows  |  Combine Columns or Cells without Losing Data  |  Round without Formula ... Super VLookup: Multiple Criteria  |  Multiple Value  |  Across Multi-Sheets  |  Fuzzy Lookup... Adv. Drop-down List: Easy Drop Down List  |  Dependent Drop Down List  |  Multi-select Drop Down List... Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Columns to Select Same & Different Cells ... Featured Features: Grid Focus  |  Design View  |  Big Formula Bar  |  Workbook & Sheet Manager | Resource Library (Auto Text)  |  Date Picker  |  Combine Worksheets  |  Encrypt/Decrypt Cells  |  Send Emails by List  |  Super Filter  |  Special Filter (filter bold/italic/strikethrough...) ... Top 15 Toolsets:  12 Text Tools (Add Text, Remove Characters ...)  |  50+ Chart Types (Gantt Chart ...)  |  40+ Practical Formulas (Calculate age based on birthday ...)  |  19 Insertion Tools (Insert QR Code, Insert Picture from Path ...)  |  12 Conversion Tools (Numbers to Words, Currency Conversion ...)  |  7 Merge & Split Tools (Advanced Combine Rows, Split Excel Cells ...)  |  ... and more

Kutools for Excel Boasts Over 300 Features, Ensuring That What You Need is Just A Click Away...

Office Tab - Enable Tabbed Reading and Editing in Microsoft Office (include Excel)

• One second to switch between dozens of open documents!
• Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
• Increases your productivity by 50% when viewing and editing multiple documents.
• Brings Efficient Tabs to Office (include Excel), Just Like Chrome, Edge and Firefox.