Whitespace use the find and replace feature to remove all double spaces from the data

Bottom Line: Learn 5 different techniques to find and remove spaces in Excel.  Spaces can cause formula errors and frustration with data preparation and analysis.

Skill Level: Intermediate

Watch the Tutorial

Watch on YouTube & Subscribe to our Channel

Download the Excel File

You can download the workbook that I use in the video, including the macro I wrote to remove blank spaces.

Whitespace use the find and replace feature to remove all double spaces from the data
Remove Blank Spaces BEFORE.xlsm

Whitespace use the find and replace feature to remove all double spaces from the data
Remove Blank Spaces AFTER.xlsm

Space Hunting

Blank spaces can be a headache for data analysts and formula writers. That's because they are not easy to spot, yet can cause frustrating calculation errors.

Whitespace use the find and replace feature to remove all double spaces from the data

After many years of working with Excel, I've become a Space Hunter. That sounds like a sci-fi movie title, but it's just someone who can quickly locate and delete superfluous spaces in cell values.

Based on my space hunting experience, here are five techniques I use frequently to clean up data.

1. Find & Replace

The first method for space hunting is to use the Find & Replace feature.

After highlighting the cells that you want to search through, go to the Home tab. Then open the Find & Select dropdown menu. Select the Replace option. The keyboard shortcut for this is Ctrl + H.

Whitespace use the find and replace feature to remove all double spaces from the data

That will bring up the Find and Replace window. On the Replace tab, place one blank space in the Find what field. Make sure there is nothing in the Replace with field. Hitting Replace All (keyboard shortcut: Alt+A) will remove any instances of a space in the data set that you selected.

Whitespace use the find and replace feature to remove all double spaces from the data

Although this method is really quick and easy, it's only useful for data where you want ALL spaces removed. However, there are times when you want to keep spaces between words. For example, if your cells contain both first and last names, you will probably want to keep the space in between those names.

That scenario leads us to our second method for space hunting.

2. The TRIM Function

The TRIM function removes all spaces in a text string, except for single spaces between words.

Below is an example of a report where some of the cells look like they are indented but the indentation is actually just extra spaces. Let's say we want to remove those spaces for uniformity's sake. We can use the TRIM function to do that.

Whitespace use the find and replace feature to remove all double spaces from the data

In a blank column, start by typing the equals sign (=) and the word TRIM, then tab into the TRIM function.

The only thing you need to identify for the TRIM function to work is the text that needs trimming. So either type the name of the cell or click on the cell that you want to trim and hit Enter. This will return the result you're looking for: text with no spaces before or after it.

Whitespace use the find and replace feature to remove all double spaces from the data

When you copy the formula down for the whole column, the result looks like this.

Whitespace use the find and replace feature to remove all double spaces from the data

As you can see, the spacing between the words remains in place, but any spacing before or after the text is removed.

With this method, we had to create a whole new column, so you would have to add the additional step of replacing the original column with the new one using Copy and Paste Values. You can then delete the column with the formulas.

3. Power Query

Like functions and formulas, queries can also be tripped up by the inclusion of a stray blank space.

Whitespace use the find and replace feature to remove all double spaces from the data

If you are unsure if blank spaces are what's causing the problem, you can click into the Editor's preview to see if there is a space or not.

Whitespace use the find and replace feature to remove all double spaces from the data

To remove the spaces, Power Query has a Trim feature found in the right-click menu. With the column that you want to fix selected, just right-click and choose Transform, and Trim.

Whitespace use the find and replace feature to remove all double spaces from the data

This trims all the blank space before/after the text string so that the query will return the correct values.

Whitespace use the find and replace feature to remove all double spaces from the data

If you are relatively new to Power Query, or could use a refresher, I recommend checking out my free webinar: The Modern Excel Blueprint.

Whitespace use the find and replace feature to remove all double spaces from the data

4. Macros and VBA

Let's look at the same example we used for the TRIM function, but this time we will use a macro to trim extra spaces from text.

Start by opening the VB Editor. Go to the Developer tab and select the Visual Basic Button. The keyboard shortcut is Alt + F11.

Whitespace use the find and replace feature to remove all double spaces from the data

I've written a simple macro that loops through each cell and replaces the value of the cell with the trimmed value. If you'd like to copy this macro, you can find it in the download file at the top of this post.

Whitespace use the find and replace feature to remove all double spaces from the data

One of the benefits of using a macro is that you can assign it to a button. Here I've assigned it to abutton on the actual worksheet.

Whitespace use the find and replace feature to remove all double spaces from the data

And here, I've added it to the ribbon by including this macro in my Personal Macro Workbook and assigning a button to it.

Whitespace use the find and replace feature to remove all double spaces from the data

That way, I can use this macro on any workbook.

One downside to using the macro method for removing blank spaces is that you cannot undo the action once the macro has been run. In that case, it might be a good idea to save the file before running the macro, if you think you might need to un-trim the data for some reason.

It's also important to note that the VBA Trim function does NOT trim extra blank spaces between words. Excel's TRIM function will remove additional spaces between words. So if there are two spaces between the first and last name, TRIM will remove the additional space. VBA's Trim will not do this. It only removes spaces from the beginning or end of the string. Thanks to Marvin (an Excel Campus Community Member) for bringing this to our attention!

Here are tutorials for how to create a Personal Macro Workbook as well as how to make Macro buttons, whether on the worksheet or in the ribbon.

How to Create a Personal Macro Workbook (Video Series)

How to Create Macro Buttons in Excel Worksheets

5. Trimming Other Characters

Sometimes after you have trimmed the spaces from your text, you might still get calculation errors. One reason for this could be that another hard-to-detect character might be in play. One such character is the non-breaking space. This is often used in HTML coding ( ) and is similar to a regular space, but would not be deleted by the TRIM function or our macro.

To determine which character might be gumming up the works, you can use the CODE function. By copying and pasting the invisible character into a blank cell, and then identifying that cell's text in the CODE function (“text” is the only argument that needs to be identified for the CODE function), you will return a code number for that character. A quick Google search of that code number will reveal what type of character you are dealing with.

Whitespace use the find and replace feature to remove all double spaces from the data

In this case, the non-breaking space character is code 160.

One way to remove that character from your text would be to use Find & Replace. You can simply copy the character and paste it into the Find what field.

Whitespace use the find and replace feature to remove all double spaces from the data

You could also use the REPLACE function to do the same thing.

One other option, since we know we want to remove the last character in the text, would be to use the LEFT and LEN functions to essentially create a customized trim feature.

LEFT returns the leftmost characters in a cell, up to a designated amount. LEN (short for Length) returns the number of characters in a cell.

So combining those two functions and subtracting 1 from the LEN to remove that unwanted character would leave just the text without the additional character.

Whitespace use the find and replace feature to remove all double spaces from the data

Conclusion

I hope this has been helpful for you. I also explain how spaces can cause errors in formulas in my video on an Introduction to VLOOKUP.

There are actually more than just five ways to go about this task, and if you'd like to add to the list, please leave a comment below. As always, you can ask questions there as well.

Thanks for following along. Until next time!

How do you use Find and Replace in Excel to remove spaces?

You can also remove spaces using the Find/Replace feature in Excel. Click CTRL+F to open the Find dialog box, then click the Replace tab. Enter one space ” ” in the Find what: field and leave the Replace with: field empty to remove all spaces.

How do you remove spaces from Find and Replace?

Remove all spaces between numbers.
Press Ctrl + Space to select all cells in a column..
Press Ctrl + H to open the "Find & Replace" dialog box..
Press Space bar in the Find What field and make sure the "Replace with" field is empty..
Click on the "Replace all" button, and then press Ok. Voila! All spaces are removed..

How do you do a Find and Replace in Excel?

Replace text or numbers with the Find & Replace.
Press Ctrl+H or go to Home > Find & Select > Replace..
In Find what, type the text or numbers you want to find..
You can further define your search: ... .
In the Replace with box, enter the text or numbers you want to use to replace the search text..
Select Replace or Replace All..

Which function is used to remove extra spaces in Excel?

The Excel TRIM function removes all the spaces in a text string, except for single spaces between words.