How to Remove Extra Spaces in Excel (5 Easy Ways)

This article describes 5 ways to remove extra spaces in Excel. Here is an animated overview:

Overview of removing extra spaces in Excel

We’ll use the dataset below, and remove the extra spaces present in the “Details” column using Excel functions and features.

Dataset for remove extra spaces in excel


Method 1 – Using Excel Functions to Remove Extra Spaces

There are a variety of functions we can use for this purpose.


1.1  – Using the TRIM Function to Remove All Extra Spaces

The TRIM function removes additional spaces from a text value.

Steps:

  • In cell D5, enter the following formula and press ENTER:

=TRIM(C5)

remove extra spaces in excel with TRIM function

The extra spaces are removed.


1.2 – Combining the TRIM, MID, FIND & LEN Functions to Remove Only Leading Spaces

Now we’ll combine the TRIM, MID, FIND, and LEN functions to remove the leading spaces from a string of text. The combination of these functions identifies the starting position of the text characters and erases the spaces before them.

Steps:

  • In cell D5, enter the formula given below and press ENTER:

=MID(C5,FIND(MID(TRIM(C5),1,1),C5),LEN(C5))

Formula Breakdown
  • LEN(C5) → returns the number of characters in a string of text, here, cell C5.
    • Output → 32
  • MID(TRIM(C5),1,1) returns the characters from the middle of a text string, given the starting position and length. Here, cell C5 is the text argument, 1 is the start_num argument, and 1 is the num_chars argument, such that the function returns the first character from the left side.
    •  Output → “J”
  • FIND(MID(TRIM(C5),1,1),C5) becomes
    • FIND(“J”,C5) returns the starting position of one text string within another text string. Here, “J” is the find_text argument while C5 is the within_text argument. The function returns the position of the numeric values in the string of text.
    • Output → 1
  • MID(C5,FIND(MID(TRIM(C5),1,1),C5),LEN(C5)) becomes
    • MID(C5,1,32) Here, the C5 cell is the text argument, 1 is the start_num argument, and 32 is the num_chars argument, such that the function returns the first character from the left side.
    • Output → “Jose,        Age:10,     Grade:4”

remove extra spaces in excel with TRIM, MID, FIND & LEN functions


1.3 – Using the CLEAN, SUBSTITUTE, and CHAR Functions to Eliminate Line Breaks

To eliminate line breaks, we can use the TRIM, CLEAN, SUBSTITUTE, and CHAR functions.

Steps:

  • In cell D5, enter the following formula and press ENTER:

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

Formula Breakdown
  • CHAR(160) → returns the character specified by the provided ASCII code. 160 represents the white space character.
    • Output → ” “
  • SUBSTITUTE(C5,CHAR(160),””) → replaces existing text with new text in a text string. Here, C5 is the text argument, CHAR(160) represents the old_text argument, and the “” is the new_text argument. So, the function substitutes white spaces with blanks.
    • Output → “Jose, Age:    10, Grade:    4  “
  • CLEAN(SUBSTITUTE(C5,CHAR(160),””)) becomes
    • CLEAN(“Jose, Age: 10, Grade: 4”) removes all nonprintable characters from text, where “Jose, Age: 10, Grade: 4” is the text argument.
    • Output → “Jose, Age:    10, Grade:    4  “
  • TRIM(CLEAN(SUBSTITUTE(C5,CHAR(160),””))) becomes
    • TRIM(“Jose, Age: 10, Grade: 4”) → removes excess spaces after the text argument, “Jose, Age: 10, Grade: 4”.
    • Output → “Jose, Age: 10, Grade: 4”

remove extra spaces in excel with TRIM, CLEAN, SUBSTITUTE, and CHAR 


Method 2 – Removing Spaces Before Numbers with the Find & Replace Feature

If combining complex formulas and expressions does not suit you, Excel’s Find and Replace command has you covered.

Steps:

  • Select the range D5:D14 >> navigate to the Find & Select drop-down on the Home tab >> choose the Replace option.

Applying Find & Replace command 

  • In the Find what field, type a blank space.
  • Leave the Replace with field empty.
  • Click the Replace All button.

entering find what and replace with and pressing replace all

All the spaces are removed.

remove extra spaces in excel with Find and Replace command


Method 3 – Using Flash Fill to Remove Extra Spaces

The Flash Fill feature recognizes a pattern and auto-fills the rest of the column without any formulas.

Steps:

  • Enter the desired output in the D5 cell (i.e. the text with spaces removed).
  • Select the range D5:D14.
  • Go to the Data tab and click Flash Fill.

using flash fill to generate output

The spaces are removed, just like that.

remove extra spaces in excel with flash fill


Method 4 – Using the Power Query Tool to Remove Spaces

The Power Query tool also makes it easy to remove spaces in Excel.

Steps:

  • Select cell B4 and press CTRL + T.
  • Click on OK to insert an Excel Table.

Inserting excel table with keyboard shortcut

  • Go to the Data tab and select From Table/Range.

Exporting table to power query editor

The Power Query editor will open.

  • Follow the steps in the GIF below.

Trim spaces in the Power Query editor

  • Click the Close & Load drop-down >> choose the Close & Load To option.

Closing and loading as table in excel

  • In the Import Data window, click OK.

Importing data to Excel as Excel Table

The final result should look like the screenshot shown below.

remove extra spaces in excel using Power Query tool


Method 5 – Using VBA Code to Remove Extra Spaces

Last but not least, If you often need to remove extra spaces, consider running the VBA Code below as a macro.

Steps:

  • Go to the Developer tab >> click the Visual Basic button.

Launching visual basic editor from developer tab

  • Go to the Insert tab >> select Module.

Inserting module

  • Copy the code below and paste it into the Module window that opens.
Sub remove_spaces()
Dim cell As Range
For Each cell In Selection.Cells
cell.Value = Trim(cell.Value)
Next cell
End Sub

VBA code for removing extra spaces in excel

Code Breakdown
  • We start by naming our subroutine remove_spaces().
  • We define the variable cell as type Range.
  • Then we use a For Loop to iterate through each value and apply the Trim function to get rid of the spaces.

VBA Code explanation

  • Copy and paste the “Details” column into the “Output” column.
  • Go to the Developer tab.
  • Click the Macros button.
  • Click Run.

Executing macro

The results should look like the image below.

remove extra spaces in excel with VBA code


Download Practice Workbook


Related Articles

<< Go Back To Remove Space in Excel | Data Cleaning in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Alok Paul
Alok Paul

Alok Paul has completed his B.Sc. in Electronics and Telecommunication Engineering from East West University. He has been working on the ExcelDemy project for more than 2 years. He has written 220+ articles and replied to numerous comments. He is experienced in Microsoft Office, especially in Excel. He also led some teams on Excel and VBA content development. He has a keen interest in Advanced Excel, Data analysis, Excel Pivot Table, Charts, and Dashboard. He loves to research... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo