Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel - convert numbers stored as text to numbers 1

Status
Not open for further replies.

hotbread

Technical User
Mar 13, 2006
42
AU
I'm trying to find a way to convert columns of numbers in text format to numbers in number format in code.

Manually this is quite easy - I just highlight the column/s, click the smart tag on the first one, and click "convert to number" which affects all cells in the selected range. I thought I could find the coding for this by simply recording a macro and clicking the smart tag, but it doesn't result in any code.

The Excel help file also has an article with the title "Convert numbers stored as text to numbers", where you copy a cell containing the value 1 and paste special into the range (in this case the selected columns) using the multiply option. This is easy enough to code in VBA, but the problem with this is that any cell in the selected range that is empty ends up displaying 0.

Does anyone know how to code in VBA what the smart tag does in converting text to numbers in a selected range?

I won't go into specifics of why I need a macro to do this, apart from saying it's impractical to do the conversion manually.

Thanks in advance!

Tim
 




Hi,

Macro record this...

enter the value 1 in a cell.

Copy.

Select the column you want to convert.

Edit/Paste Special - MULTIPLY.

Skip,

[glasses] [red][/red]
[tongue]
 
Thanks for your input Skip, but as I said I have already tried this and it leads to a different problem where blank cells end up displaying 0, so this solution is unsuitable.
 



for column "A", modify as needed...
Code:
For each r in range(cells(1,"A"), cells(cells.rows.count, "A").end(xlup))
  With r
   if len(.value)>0 then .value = .value * 1
  end with
next

Skip,

[glasses] [red][/red]
[tongue]
 
Thanks Skip.... that code worked perfectly!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top