VBA to convert from Text to Number

chaddres

Board Regular
Joined
Jun 14, 2014
Messages
143
Office Version
  1. 365
Platform
  1. Windows
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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Try

Code:
For Each xCell In Range("A2").CurrentRegion
    If IsNumeric(xCell) Then xCell.Value = Val(xCell.Value)
Next xCell
 
Upvote 0
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]
 
Upvote 0

Forum statistics

Threads
1,214,285
Messages
6,118,673
Members
448,845
Latest member
MrEbzz

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
Back
Top