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 June 20, 2018)

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

Non-breaking Em Dashes

Need an em dash to be "sticky" on both ends of the dash? Word doesn't provide such formatting, but there are a few ...

Discover More

Hiding and Unhiding Rows

When building a worksheet, you may need to hide some of the rows or unhide other, previously hidden, rows. It's easy to ...

Discover More

Forcing a Macro to Run when a Worksheet is Recalculated

Normally a macro is only calculated when you specifically tell Excel to calculate it. Some macros need to be calculated ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (menu)

Shortcut for Selecting a Data Range

Want to select all the data in a contiguous section of a worksheet? The shortcut discussed in this tip makes it very easy.

Discover More

Ensuring Standard Units During Data Entry

Need to make sure that information entered in a worksheet is always in a given unit of measurement? It's not as easy of a ...

Discover More

Limiting Entry of Prior Dates

Want to establish a "bottom limit" on what dates can be entered in a cell? This tip presents two different ways you can ...

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}] 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 eight minus 6?

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.