Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA: Convert Text to Number

I have columns of numbers that, for whatever reason, are formatted as text. This prevents me from using arithmetic functions such as the subtotal function. What is the best way to convert these "text numbers" to true numbers?

Here is a screenshot of the specific issue: Error

I've tried these snippets to no avail:

Columns(5).NumberFormat = "0" 

and

 Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _         :=False, Transpose:=False 
like image 802
aLearningLady Avatar asked Apr 21 '16 13:04

aLearningLady


People also ask

How do I convert a text string to a number?

Use Paste Special and Multiply Select the cells that have numbers stored as text. On the Home tab, click Paste > Paste Special. Click Multiply, and then click OK. Excel multiplies each cell by 1, and in doing so, converts the text to numbers.

How do I convert Excel text to number?

Select all the cells that you want to convert from text to numbers. Click on the yellow diamond shape icon that appears at the top right. From the menu that appears, select 'Convert to Number' option.


1 Answers

Use the below function (changing [E:E] to the appropriate range for your needs) to circumvent this issue (or change to any other format such as "mm/dd/yyyy"):

[E:E].Select With Selection     .NumberFormat = "General"     .Value = .Value End With 

P.S. In my experience, this VBA solution works SIGNIFICANTLY faster on large data sets and is less likely to crash Excel than using the 'warning box' method.

like image 86
aLearningLady Avatar answered Oct 12 '22 02:10

aLearningLady