VBA to convert from Text to Number

chaddres

Board Regular
Joined
Jun 14, 2014
Messages
76
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!
 

Some videos you may like

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

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,194
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]
 

Watch MrExcel Video

Forum statistics

Threads
1,122,819
Messages
5,598,293
Members
414,223
Latest member
Accountant2B

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