VBA to convert to number

steelervince

Board Regular
Joined
May 29, 2007
Messages
83
I have a string of imported data which comes into an existing spreadsheet. It cannot be changed prior to importing as the data is provided by an external vendor.

When the data is imported into my spreadsheet the Error Checking tag comes on, after I select the cells where certain numeric data has been entered, stating that this is a range of Numbers Stored as Text. If I click on the Error Checking tag and select the Convert to Number option and then enter my VLOOKUP function I get the desired answer. If I do not convert the text to numbers then my VLOOKUP function does not work at all. The cell just displays the function text itself instead of the desired answer.

Here is what I am requesting:

Assistance with writing the VBA code to accomplish the manual task of converting the text to numbers. I have tried to format using the Ribbon without success.

Any ideas out there?
 
Being new to VBA or at least learning as I go, my job has many hats to it so I do not get to spend much time sitting down and really study why VBA code does what it does.

That being said I have a general understanding of some of the commands and can sometimes adjust code to get the desired outcome.

I am not sure that answers your question, but thank you for your assistance!
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Another possibility

Code:
Sub nfmt()
With ActiveSheet.UsedRange
    .Value = Evaluate(.Address & "*1")
End With
End Sub

Fascinating.

I'm trying to find a smooth way of "converting to numbers" also. I googled and found this...then I googled this and can find no meaningful info (lots of sketchy stuff, tho) on this function.

Can you elaborate on what's going on here?
 
Upvote 0
Basically it is multiplying the text values by one to coerce them into numbers. Why that worked in this case and .Value=.Value didn't I have no idea :)
 
Upvote 0
Try

Code:
Sub nfmt()
With ActiveSheet.UsedRange
    .Value = .Value
End With
End Sub

Funny -- for me, this is the code that did the trick.

The second version put #ERROR into every cell.

Basically it is multiplying the text values by one to coerce them into numbers
Gosh what a cool trick. I didn't know you could do that.
 
Upvote 0
Both worked for me. However,

.Value = .Value is better because Real "text" data produces an error with

.Value = Evaluate(.Address & "*1")

For me anyway.

My Thanks Also to Peter.
 
Upvote 0
Been looking for this for a while and this has given me the answer so thanks.
This was the code that did it

Sub nfmt()
With ActiveSheet.UsedRange
.NumberFormat = "General"
.Value = .Value
End With
End Sub
 
Upvote 0
Yeah! The code worked for me as well.....just that it removes ALL the formulas from the current sheet and converts them into number.
 
Upvote 0
Yeah! The code worked for me as well.....just that it removes ALL the formulas from the current sheet and converts them into number.

Where are your text numbers located at? The code can be modified to restrict its actions to a specified range as opposed to the entire sheet. For example...

Code:
With Columns("C")
  .Cells.NumberFormat = "General"
  .Value = .Value
End With

Just change the Columns argument to the column or columns letter range reference, or use Range(arg) where arg is as specific cell or cells reference, depending on your actual setup.
 
Last edited:
Upvote 0
Hi Rick,

Thank you very much.
My data would be pulled from one of the tools my org uses and exports data in several columns. Current macro helps me as there will be no formulas in the report extracted.

Suggestions are noted! Will modify the macro accordingly.

Regards,
Ganesh K
 
Upvote 0
In order to optimized and to another vision for anyone that couldn't fixed the famous error " Number Stored as Text"

VBA Code:
Sub converttonumber()
'Convert numbers (stored as text) to number in a range, in this example in Range A1 to last cell with data

Dim I As Range
For Each I In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
I.Value = I.Value * 1
Next I
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,861
Members
449,052
Latest member
Fuddy_Duddy

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