VBA to convert from Text to Number

chaddres

Board Regular
Joined
Jun 14, 2014
Messages
91
I have a spreadsheet with multiple numbers stored as text. I wish to convert all of the numbers stored as text to numbers. I wrote this code:

Range("A2").Select
Selection.CurrentRegion.Select
If IsNumeric(xCell) Then
For Each xCell In Selection
xCell.Value = Val(xCell.Value)
Next xCell
End If

My problem is that it converts all of my text to 0's when it converts the numeric text values to numbers.

Thank you!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
For Each xCell In Range("A2").CurrentRegion
    If IsNumeric(xCell) Then xCell.Value = Val(xCell.Value)
Next xCell
 

vds1

Well-known Member
Joined
Oct 5, 2011
Messages
1,200
Hello,

Just came to my mind.. what if i do this way.. Obviously Peter has better solution..

Code:
[COLOR=#0000ff]Sub[/COLOR] Without_Loop()
[COLOR=#0000ff]With[/COLOR] Range("XFD1")
    .Value = 1
    .Copy
[COLOR=#0000ff]End With[/COLOR]
[COLOR=#0000ff]With[/COLOR] Range("A2").CurrentRegion
    .PasteSpecial Paste:=xlPasteFormats
    .PasteSpecial Paste:=xlPasteValues, Operation:=xlMultiply
[COLOR=#0000ff]End With[/COLOR]
Range("XFD1").Value = ""
[COLOR=#0000ff]End Sub[/COLOR]
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,008
Messages
5,834,855
Members
430,324
Latest member
bosphoruskid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top