How to remove ALL spaces?

I need a macro to remove ALL spaces from numbers that are formatted as:
123 456 789 123
789 654 123 834
I need them like this:
12345678123
789654123834

I will format them after the spaces are removed to "### ### ### ###" so I can read them.
I know this is easy for some but I cannot make it happen.
Thanks,
Jimi ShermanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
Hello,

maybe something like this. Select the cells and run the macro

Sub test()
Dim cel As Range

For Each cel In Selection
    cel = WorksheetFunction.Substitute(cel.Value, " ", "") * 1
Next cel

End Sub

Open in new window


cheers, teylyn
0
Jimi ShermanAuthor Commented:
That did not work it stop as debug on row 5 of your code.
0
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPDesigner and DeveloperCommented:
Have you tried the Replace() VBA function

see: http://www.techonthenet.com/excel/formulas/replace_vba.php

This works for me:

Sub test()
Dim cel As Range

For Each cel In Selection
    cel = Replace(cel.Value, " ", "")
Next cel

End Sub

Open in new window

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
Hmm. Worked fine for me in Excel 2010. What's your version?
Maybe your regional settings require a semicolon instead of a comma?
0
Martin LissOlder than dirtCommented:
No need to select.


Sub RemoveSpaces()
Dim cel As Range

For Each cel In Range(Cells(1, 1), Cells.SpecialCells(xlCellTypeLastCell)) 
    If cel.Value <> "" Then
        cel.Value = Replace(cel.Value, " ", "")
    End If
Next
End Sub

Open in new window

0
byundtMechanical EngineerCommented:
It is possible that your cells might contain non-breaking spaces (ASCII 160). And depending on how big the selection is, it might take a long time to update the values one at a time. Both issues are addressed in the snippet below:
Sub test()
Dim cel As Range, rg As Range
Dim i As Long, j As Long, nCols As Long, nRows As Long
Dim s As String
Dim v As Variant
Set rg = Intersect(Selection.Cells, ActiveSheet.UsedRange)
v = rg.Value
nCols = rg.Columns.Count
nRows = rg.Rows.Count
For i = 1 To nRows
    For j = 1 To nCols
        s = v(i, j)
        s = Replace(Replace(s, Chr(160), ""), " ", "")
        If IsNumeric(s) Then v(i, j) = Val(s)
    Next
Next
rg.Value = v
rg.NumberFormat = "000 000 000 00#"     'Optional step: format the selection to display spaces between triplets
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jimi ShermanAuthor Commented:
That works great and thanks for the extra step in formatting the numbers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.