How can I automate the removal of leading zeros and commas in Excel fields?

In an Excel spreadsheet, using VBA or any other script, I would like to remove leading zeros in field in a specific column, and I would also like to replace commas with nothing.
100questionsAsked:
Who is Participating?
 
Glenn RayConnect With a Mentor Excel VBA DeveloperCommented:
To automate the above steps in VBA (this example applies to column A):
Sub Convert_To_Numbers()
    Dim rng As Range
    Dim cl As Object
    Columns("A:A").Select
    Selection.Replace What:=",", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows
    Set rng = Range("A1:A" & Cells.SpecialCells(xlLastCell).Row)
    rng.NumberFormat = "General"
    For Each cl In rng
        cl.Formula = Trim(cl.Formula)
    Next cl
End Sub

Open in new window


-Glenn
0
 
Glenn RayExcel VBA DeveloperCommented:
It sound like the column contains numeric data but is formatted as text.  If this is the case I would do the following:

1) Highlight all the data in the column and use the Replace function to replace commas with nothing.
remove commas2) Highlight all the data, click on the alert icon, and then select "Convert to Number"
convert to number
Regards,
-Glenn
0
 
100questionsAuthor Commented:
Thanks Glenn.  Seems to work after I enter the correct columns and fix the range to what I need.
0
All Courses

From novice to tech pro — start learning today.