How do I tell the function to look at all the cells in column A?
Main Topics
Browse All TopicsI need some code to remove any spaces in a string. I have a list of accounts in a column A and each cell has a bunch of numbers in it.
Example
Cell A1 100 00 0000 00
Cell A2 2222 2 222 22
So there is no pattern to the spaces, but I need something that go through each cell in column A and remove all the spaces.
So my results would look like this
Cell A1 10000000000
Cell A2 2222222222
Thanks for the help,
Montrof
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi montrof,
Try it without a macro. Select your column, then use the Edit...Replace menu item. Enter a single space in the Find what field and nothing at all in the Replace with field. Click OK, and the job should be done.
Here's a macro that works on the selected range (could be an entire column):
Sub DeleteBlanks()
Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
Cheers!
Brad
montrof,
Are the spaces really there in your strings? If you format the cell with a custom number format like ### ## #### (like a social security number) there will be spaces in the printout and screen display, but not in the value stored in the cell. Find and Replace won't get rid of them--you'll have to change the cell format instead.
Brad
I assume that your account numbers in Column A are values not formulas?
I tried Brad's code and it worked fine in Excel 2003 but it threw up an error in Excel 2000, but when I cut the Replace method down to three arguments as below it worked ok
Sub Rep()
Dim MyRange As Range
Set MyRange = Intersect(Columns("a:a"), ActiveSheet.UsedRange)
MyRange.Replace What:=" ", Replacement:="", LookAt:=xlPart
End Sub
If this still doesn't work then this more complex RegExp sub may be worth a shot
Sub BlankDel()
Dim Reg
Dim MyRange As Range
Dim Cel As Range
Set MyRange = Intersect(Columns("a:a"), ActiveSheet.UsedRange)
Set Reg = CreateObject("vbscript.reg
Reg.Global = True
For Each Cel In MyRange
Reg.Pattern = "\s+"
Cel.Value = Reg.Replace(Cel.Value, "")
Next
End Sub
Cheers
Dave
I found this macro posted and it seems to work
Sub RemoveSpaces()
'Declare variables
Dim ws As Worksheet
Dim rng As Range
Dim arr As Variant
Dim r As Long
'Rng refers to the used range in the first column
'of the active sheet
Set ws = ActiveSheet
Set rng = ws.UsedRange.Columns(1)
'Read the values of rng into array
arr = rng.Value
'Loop through all rows in the array
For r = 1 To rng.Rows.Count
'Remove spaces
arr(r, 1) = WorksheetFunction.Substitu
Next r
'Write values back to sheet
rng.Value = arr
'Release object variables
Set rng = Nothing
Set ws = Nothing
End Sub
hello montrof,
I hope this will help you.. I created a simple macro to remove blanks. this is to ensure that the value will be considered as text not numbers
Sub removeBlanks()
Dim strHolder As String
Dim icount As Integer
Dim strChar As String
While ActiveCell.Value <> ""
strHolder = ActiveCell.Value
ActiveCell.Value = ""
For icount = 1 To Len(strHolder)
strChar = Mid(strHolder, icount, 1)
If strChar <> " " Then
ActiveCell.Value = ActiveCell.Value & strChar
End If
Next
ActiveCell.Value = "=""" & ActiveCell.Value & """"
ActiveCell.Offset(1, 0).Select
Wend
ActiveCell.Offset(-1, 0).Select
Range(Selection, Selection.End(xlUp)).Selec
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Business Accounts
Answer for Membership
by: shivsaPosted on 2004-01-23 at 14:41:10ID: 10188371
check this.
ShowCode.A sp?ID=5613
http://www.freevbcode.com/