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. ...


Filling A Drawing Object

Creating a drawing object can be just the first step in putting together your masterpiece drawing. This tip explains how ...

Discover More

Putting Something in Every Cell of a Table

Need to make sure that all the cells of a table have something in them? It's easy to do with a handy little macro.

Discover More

Generating a Keyword Occurrence List

Need to pull a list of words from a range of cells? This tip shows how easy you can perform the task using a macro.

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (menu)

How Many Rows and Columns Have I Selected?

Want a quick way to tell how may rows and columns you've selected? Here's what I do when I need to know that information.

Discover More

Deleting Old Data from a Worksheet

If you keep on-going data in a worksheet, some of your dataâ€"over timeâ€"may need to be deleted. If you have an ...

Discover More

Limiting Number of Characters in a Cell

Need to limit the number of characters that can be entered into a cell? One easy way to do it is through the use of Data ...

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 seven minus 2?

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.