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

Written by Allen Wyatt (last updated April 8, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003


6

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, " ", "")
    Next
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.

Note:

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

MORE FROM ALLEN

Unhiding Multiple Worksheets

You can hide a bunch of worksheets at the same time, but Excel makes it impossible to unhide a bunch at once. You can, ...

Discover More

Highlighting Every Thousandth Character

Not satisfied with the detail provided by the Word Count feature in Word? Perhaps you want to actually know where every ...

Discover More

Seeing a Worksheet Thumbnail in Windows

When you save a workbook, you have the opportunity to save a thumbnail image that can be displayed within Windows. Here's ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (menu)

Forcing Input to Uppercase

If you type information into a workbook, you may want to make sure that what you type is always stored in uppercase. ...

Discover More

Changing Multiple Cells at Once

Excel includes several different methods of editing information in your cells. If you want to edit multiple cells all at ...

Discover More

Finding Unused Names

After months or years of naming things (such as cell ranges), you may find your workbook cluttered with a bunch of names ...

Discover More
Subscribe

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

View most recent newsletter.

Comments

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 nine minus 5?

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
https://excelribbon.tips.net/T010741_Removing_Spaces
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

@Alan
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 .........
....https://excelribbon.tips.net/T010741_Removing_Spaces.html


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
Subscribe

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.