|
Home > Formulas > Text > Remove Spaces Remove Spaces in Excel CellsHow to remove spaces in Excel, using TRIM, SUBSTITUTE, find and replace, macros. Videos, written steps, sample file. Avoid problems with sort, filter, lookup |

In some Excel worksheets, there might be extra spaces in some cells, that you want to remove. For example, imported data, from a website, or another computer system, might contain leading or trailing spaces, or extra spaces between words.
Those extra spaces can cause problems when you try to sort or filter the data, if you're using lookup functions, such as VLOOKUP, MATCH or XLOOKUP. Fortunately, Excel has built-in functions and commands that can help you find and remove excess spaces.
In the sections below, you'll see how to trim spaces in Excel, with:
--1) TRIM function
--2) SUBSTITUTE function
--3) CLEAN function
--4) Find and replace command
--5) Excel macros
With the TRIM and SUBSTITUTE functions, you can put the formulas in a helper column, beside the original text. This will create a column with the trimmed text, and the original data will remain unchanged.
The formula methods are helpful if the original data will change, or you copy and paste new data onto the worksheet frequently.
The formula results could be copied, and pasted as values into another location, use a Paste Special command.
If you use the Find and Replace command, or one of the Excel macros, the original data will be changed.
As a precaution, be sure to make a backup copy of the original data, before you use one of these methods to trim spaces in the data.
The Excel TRIM function can help with the cleanup of text that you've downloaded from a website, or imported from another application.
In this video you'll see how to use the TRIM function to:
There are written steps below the video.
The TRIM function has the following syntax:

The TRIM function will remove all the space characters at the start and end of a text string.
For example, in cell C5 in the screenshot below, there are:
In cell E5, the LEN function calculates how many characters are in cell C5, and the result is 17 characters.
The TRIM formula in cell C7 removes those 4 spaces: =TRIM(C5)
In cell E7, the LEN function calculates the number of characters that are in cell C7, and the result is 13 characters -- the leading space characters and trailing space characters were removed.

You can also use the TRIM function to remove extra space characters between words in a text string.
In the screenshot below, there are 3 extra spaces between the words in cell C5. In cell E5, the LEN function calculates how many characters are in cell C5, and the result is 20 characters.
The TRIM function in cell C7 removes those extra spaces, as well as the 2 spaces at the start and 2 spaces at the end of the text string.
=TRIM(C5)
In cell E7, the LEN function calculates how many characters are in cell C7, and the result is 13 characters -- the extra spaces between the words, and the spaces at the start and end were removed.

It's important to know that the TRIM function only removes regular spaces. It does NOT remove some space characters, such as non-breaking spaces copied from a website. The video below shows how to remove those characters.
For example, in this screenshot, cell C5 contains one non-breaking space. In cell E5, the LEN function calculates how many characters are in cell C5, and the result is 14 characters.
That non-breaking space is NOT trimmed by the TRIM function: =TRIM(C5)
In cell E7, the LEN function calculates how many characters are in cell C7, and the result is 14 characters -- the non-breaking space was not removed in the trimmed values.

To remove the non-breaking space characters, we can use the SUBSTITUTE function, combined with the CHAR function. Here is the formula in cell C5:

The SUBSTITUTE function can remove a specific character, and replace it with a different character, or an empty string (nothing). The SUBSTITUTE function has the following 4 arguments in its syntax:
We'll use the first three arguments in our formula
Tip: To remove only a specific occurrence of the old text, use the 4th argument, instance_num)
Behind the scenes in Excel, every character has a code number. For example:
In our formula, CHAR(160) specifies that we want to remove non-breaking space characters.
Tip: To figure out what character is at the start of a cell, use the Excel CODE function. It returns the code number for the first character in a text string.

If you want to clean cells that have extra spaces, AND non-breaking spaces, you can use the TRIM function, combined with the SUBSTITUTE function, in a nested formula.
Here is the formula in cell C11:
The SUBSTITUTE formula removes any non-breaking spaces, and TRIM removes all other spaces, except for a single space between words.

In this video you'll see how to use SUBSTITUTE to change a region name in a report title, remove non-printing characters, and substitute a space character
There are more examples on the REPLACE or SUBSTITUTE page.
In addition to non-breaking characters, there are other non-printing characters in Excel, and some of those could also be in data imported from an external source.
To remove some non-printable characters, you can use the CLEAN function, alone, or combined with other functions.
To test the CLEAN function, I created sample text which contains these non-printing characters:
In columns D, E and F, formulas check for characters 10, 13 and 160, and return the position in which there were found (20, 30 and 1)
In rows 6, 7, and 8, three different formulas try to clean the text, to remove non-printing characters, non-breaking spaces, and extra spaces.
So, in your Excel worksheets, use formulas that combine CLEAN, SUBSTITUTE and TRIM, if your data contains different types of non-printing characters, and extra spaces to remove.

If you need to remove extra spaces between words on an Excel worksheet, the Find and Replace command can help you do that, without the need for any formulas.
To remove extra spaces between words on an Excel worksheet, using the Find and Replace command, follow these steps:

Excel shows a confirmation message, with the number of replacements that were made.
Click the OK button, to close the message, but don't close the Find and Replace window!
The cleanup might not be "All done".
When you're using this method, to replace two space characters with one space character, it's important to repeat the final step, and click the Replace All button again.
In some cells, there could be three or more space characters between two words, or at the start or end of the cell's text.

In this video, you'll see how to use the Find and Replace command, to clean the data in a contact list. There are written steps, and more examples, on the Excel Find and Replace page.
Video Timeline
Instead of using TRIM formulas to remove spaces, you can use a VBA Trim function in an Excel macro, to remove leading spaces, trailing spaces, and extra spaces.
The advantage of using a macro is that this is a quick clean up task, and you don't need to write formulas, or use extra columns on your worksheet.
To add this Excel VBA macro to your workbook, copy the code shown below. Then, paste the code into a regular code module in your workbook.
To run the macro, follow these steps:
Tip: You could add a Trim button on the worksheet, to run the macro, if data needs to be cleaned frequently.
Sub TrimExtraSpaces_App() Dim rng As Range Set rng = Selection rng.Value = Application.Trim(rng.Value) End Sub
In Excel VBA, there are two Trim functions:
Function Similarities
Both functions do the same task -- they remove all spaces from the input, except for a single space between words.
Although the results are the same, here are the differences betwee the two Trim functions:
Application.Trim can accept a string, or a range of cells, as its input, and it returns a variant data type. In the macro shown above, the entire selection had its values trimmed with a single step in the code:
WorksheetFunction.Trim can only accept a string as its input, and returns a string data type. To use this function, the code needs to loop through all of the cells in the selection, and trim each cell's value separately.
In the macro shown below, the cells are trimmed in a For Each...Next loop
Sub TrimExtraSpaces_Wks() Dim rng As Range
Dim c As Range
Set rng = Selection
For Each c In rng
c.Value = _
WorksheetFunction.Trim(c.Value)
Next c End Sub
Last updated: September 25, 2024 2:34 PM