Solved

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

Posted on 2014-07-22
3
216 Views
Last Modified: 2014-07-23
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.
0
Comment
Question by:100questions
  • 2
3 Comments
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40212521
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
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40212569
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
 

Author Comment

by:100questions
ID: 40214132
Thanks Glenn.  Seems to work after I enter the correct columns and fix the range to what I need.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question