Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Removing Spaces.

Removing Spaces

by Allen Wyatt
(last updated April 8, 2022)


Do you have a lot of data that contains spaces, and you need to remove those spaces? Perhaps you imported it from another program, or the spaces were entered by mistake. For example, you may have a large number of policy numbers in a worksheet, and there are spaces in the policy numbers. If you want to remove those spaces, there are two approaches you can use.

The first approach is to use the SUBSTITUTE function. Let's say that a policy number is in cell A5. In cell B5 you could use this formula:

=SUBSTITUTE(A5," ","")

The result is that cell B5 contains the policy number with all the spaces removed.

The second approach works well if you have a lot of cells containing spaces, and you want to remove them in one step. Create the following macro:

Sub NoSpaces()
    Dim c As Range

    For Each c In Selection.Cells
        c = Replace(c, " ", "")
End Sub

Select the cells you want to modify, and then run the macro. It examines each cell in the selected range, removing any spaces in that range. The result is then placed back in the same cell.


If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3037) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Removing Spaces.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...


Changing a Macro Description

Part of documenting macros is to provide a good, succinct description of what they do. Changing the description of an ...

Discover More

Black and White Blues

Getting a chart looking its best on a black-and-white printer can be a challenge. This tip examines different ways you ...

Discover More

Complex Searches for Documents

When working with lots of documents, you may have need from time to time to discover which of those documents contain ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (menu)

Inserting Dashes between Letters and Numbers

If you need to add dashes between letters and numbers in a string, the work can quickly get tedious. This tip examines ...

Discover More

Creating a Shortcut for Pasting Values

Excel's Paste Special command is used quite a bit. If you want to create some shortcuts for the command, here's some ways ...

Discover More

Cleaning Up Lists

When you have huge amounts of data you need to check for matches, Excel may not be the best tool to use. If you can fit ...

Discover More

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.


If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is six more than 7?

2020-03-11 14:50:55

Alan Elston

I think, as I recall , the first few times I used it , I saw the spaces vanish, assumed it was working, and only some time later noticed that all the cells were the same value.
It always works with the IF({1}, as far as I have heard so far , in any Excel Version, new or old.
Its easy to forget/ not notice, especially now since 2016+, - the coercing tricks often aren’t needed anymore. The change causes a lot of confusion: - It also causes problems the other way around as well: Suddenly a macro is not working anymore when used in a newer version because the Evalute(” “) suddenly returns an array when it didn’t before.

2020-03-11 10:12:04

Willy Vanhaelen

Hi Allan,

Indeed I have been to hasty and tested it not enough :-(
You are right, Substitute must be embedded in an IF({1}, ... But then it works in my Excel 2007.

Sub NoSpaces()
Selection = Evaluate("If({1},SUBSTITUTE(" & Selection.Address & ", "" "", """"))")
End Sub

2020-03-11 05:21:10

Alan Elston

Hi Willy.
I don’t doubt your word for 1 second.
It is very strange. I have access just now to about a dozen machines with various versions of Excel 2002 – 2010. I just re checked a few of them, and still get the same results:-
For a selection more than one cell I need a coercion, as discussed at the other of Allen’s space removing blogs
So, for example, I have to do something like this coercion to get more than a single value out:-
Evaluate("If({1},SUBSTITUTE(" & Selection.Address & ", "" "", """"))")
Without the coercion , then usually for Excel 2013 and lower , you get just the result out for the first cell , in all the cells.

About a half a dozen people get similar results to me. That ties up with what I have been seeing often in forums for similer issues:

But I have not tried infinite test data. If you have a file where it is working for you, can you pass it to me , then I will see what happen on my Excels. ( Just click on my name at the left and you should get my E-Mail). I would be interested to investigate this. I keep seeing inconsistent results since Microsoft stated meddling with these things. So maybe we have come across another peculiar effect…..

Alan Elston

2020-03-10 06:59:31

Willy Vanhaelen

I have Excel 2007 and it works there. I suppose it works in older versions as well.

2020-03-09 07:12:54

Alan Elston

That will likely only work for Excel 2016 + , Willy .........

2020-02-22 11:25:21

Willy Vanhaelen

With contiguous ranges you can use this one-liner:

Sub NoSpaces()
Selection = Evaluate("SUBSTITUTE(" & Selection.Address & ", "" "", """")")
End Sub

This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.