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.
Written by Allen Wyatt (last updated April 8, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003
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:
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.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
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 MoreExcel allows you to easily paste information into a worksheet, including through simply dragging and dropping the ...
Discover MoreThere are several things to try when finding unknown links in Excel.
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-03-11 14:50:55
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
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
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
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments