How to Convert Text to Number in Excel with VBA

 

How to Identify Numbers Formatted as Text

  • Take a look at this dataset. We have some numbers in a column.

dataset for converting text to number using VBA in excel

  • Though we have numbers in the column, they all are left-aligned like the text.
  • Click any cell. You will find a warning box.

click any cell to check number or text in excel

  • Hover over the box. You will see this message.

values are in text form instead of number

  • It shows that the cell is formatted as text.

How to Convert Text to Number in Excel with VBA: 3 Examples


Method 1 – VBA Code with the Range.NumberFormat Method to Convert Text to Number in Excel

Steps

  • Press Alt + F11 to open the VBA editor.
  • Click on Insert, then on Module.

insert module for VBA editor

  • Insert the following code:
Sub ConvertTextToNumber()
With Range("B5:B14")
.NumberFormat = "General"
.Value = .Value
End With
End Sub
  • Save the file.
  • Press Alt + F8. It will open the Macro dialog box.

macro dialog box to convert text to number using vba in excel

In the code,

Firstly, we created a subprocedures named ConvertTextToNumber

Then, With Range("B5:B14") this part selects the range B5:B15.

Afterward, .NumberFormat = "General" this part changes the number format to General.

Finally, .Value = .Value this part keeps the value intake.

  • Select ConvertTextToNumber and click on Run.

convert text to number in excel using Range.NumberFormat

  • This code will convert our text to numbers.

Read More: How to Convert String to Number in Excel VBA


Method 2 – VBA Code with a Loop and CSng to Convert Text to Number

Steps

  • Press Alt + F11 to open the VBA editor.
  • Click on Insert, then on Module.

insert module for VBA editor

  • Copy the following code:
Sub ConvertUsingLoop()
For Each r In Sheets("Loop&CSng").UsedRange.SpecialCells(xlCellTypeConstants)
If IsNumeric(r) Then
r.Value = CSng(r.Value)
r.NumberFormat = "General"
End If
Next
End Sub
  • Save the file.
  • Press Alt + F8. It will open the Macro dialog box.

vba macro dialog box to run vba code

In the code,

Firstly, we created a subprocedures ConvertUsingLoop.

Then,

For Each r In Sheets("Loop&CSng").UsedRange.SpecialCells(xlCellTypeConstants)

If IsNumeric(r) Then

r.Value = CSng(r.Value)

r.NumberFormat = "General"

this loop changes the format selected range into General format.

  • Select ConvertUsingLoop and click on Run.

text converted to number in excel using Loop & Csng


Method 3 – Convert Text to Numbers for Dynamic Ranges in Excel

Steps

  • Press Alt + F11 on your keyboard to open the VBA editor.
  • Click on Insert and then on Module.

insert module for VBA editor

  • Insert the following code:
Sub ConvertDynamicRanges()
With Range("B5:B" & Cells(Rows.Count, "B").End(xlUp).Row)
.NumberFormat = "General"
.Value = .Value
End With
End Sub
  • Save the file.
  • Press Alt + F8. It will open the Macro dialog box.

macro dialog box for vba codes

In the code,

Firstly, we created a subprocedures ConvertDynamicRanges.

Then, With Range("B5:B" & Cells(Rows.Count, "B").End(xlUp).Row) this part counts the cells in column those have values.

Afterward, .NumberFormat = "General" this part changes those cell’s format into General format.

And, .Value = .Value this part keeps the value constant.

  • Select ConvertDynamicRanges and click on Run.

text converted to number for dynamic ranges using VBA in excel


Things to Remember

✎ Here, we are using column B for our dataset. If your data is in a different column, change the range of cells in VBA codes accordingly.

✎ The VBA codes will work only on the active sheet.


Download the Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
A.N.M. Mohaimen Shanto
A.N.M. Mohaimen Shanto

A.N.M. Mohaimen Shanto, a B.Sc. in Computer Science and Engineering from Daffodil International University, boasts two years of experience as a Project Manager at Exceldemy. He authored 90+ articles and led teams as a Team Leader, meticulously reviewing over a thousand articles. Currently, he focuses on enhancing article quality. His passion lies in Excel VBA, Data Science, and SEO, where he enjoys simplifying complex ideas to facilitate learning and growth. His journey mirrors Exceldemy's dedication to excellence and... Read Full Bio

4 Comments
  1. Thank you SO much! VBA #3 worked perfectly for what I needed! Appreciate this simple guide

  2. Hi, everywhere I look, I see the same solution… but it’s not working for me… Is it not working on 2016?

    With Range(“B5:B” & Cells(Rows.Count, “B”).End(xlUp).Row)
    .NumberFormat = “General”
    .Value = .Value
    End With

    The code stop at .value = .value

    Thank you for your time

  3. Hi Everyone, How do i convert text to numbers in multiple columns. For example Column N, P. AB, CC?
    Thank you in advance

    • Hello Polina. Thanks for asking. Basically, you have to change the cell references here.
      Suppose you have data in the following ranges:
      1. N3:N13
      2. P1:P19
      3. AB13:AB20
      4. CC10:CC20

      Just use the following code:

      Sub ConvertTextToNumber()
      With Range(“N3:N13”)
      .NumberFormat = “General”
      .Value = .Value
      End With

      With Range(“P1:P19”)
      .NumberFormat = “General”
      .Value = .Value
      End With

      With Range(“AB13:AB20”)
      .NumberFormat = “General”
      .Value = .Value
      End With

      With Range(“CC10:CC20”)
      .NumberFormat = “General”
      .Value = .Value
      End With

      End Sub

      There are various ways. But this is the simplest.

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo