VBA code to convert Text to Number

Anonymous
2012-03-27T02:37:30+00:00

HI,

Do everyone knows what is the VBA code to convert Text to Number?  like '01116 convert to 1116.

Best Regards,

Lawrence

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
Anonymous
2012-03-27T13:58:48+00:00

Hi,

try this..

convert  text number to number

Sub ConvertTextNumberToNumber()

For Each WS In Sheets

On Error Resume Next

For Each r In WS.UsedRange.SpecialCells(xlCellTypeConstants)

If IsNumeric(r) Then r.Value = Val(r.Value)

Next

Next

End Sub

Was this answer helpful?

60+ people found this answer helpful.
0 comments No comments
Answer accepted by question author
HansV 462.7K Reputation points MVP Volunteer Moderator
2012-03-28T07:47:45+00:00

Some of the cells had a value prefixed with ' to make them text, and they had also been formatted as Text. The solution, as already provided by others, is to format the cells as General.

Was this answer helpful?

30+ people found this answer helpful.
0 comments No comments

31 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-03-26T02:59:19+00:00

    MS Office (excel) 2010:  version 14.0.  ---

    Try this ....

    Sub Testit()

         wkText = "16.3"  

          wkNumber = wkText * 1   'Multiply by 1 -- gives a number

    End Sub

    run with debugger (F8) to step through each line.

    You'll notice that wkText is indeed a text value of "16.3"

    The value of wkNumber will be 16.3 -  a number

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2013-12-13T05:21:34+00:00

    The best and quick way I find is to use text to column. It is lightning quick!

    NOTE: you need to select your range before running this code and it works only on one column at a time.

    you can define your range within your sub if it is known, eg range(range("a1").range("a1").end(xldown)).select

    Sub Txt_to_No()

        On Error GoTo errhndlr

         Selection.TextToColumns Destination:=Selection, DataType:=xlDelimited, _

                TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _

                Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _

                :=Array(1, 1), TrailingMinusNumbers:=True

         Exit Sub

    errhndlr:

        MsgBox "something something vbexcclamation, "something"

    End Sub

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2012-03-28T04:50:42+00:00

    Hi Lawrencetan

    try this also..

    converts all cells that contain numbers to format General

    Sub ConvertTextNumericToNumber2()

    For Each WS In Sheets

    On Error Resume Next

    For Each r In WS.UsedRange.SpecialCells(xlCellTypeConstants)

    If IsNumeric(r.Value) Then

    r.Value = Val(r.Value)

    r.NumberFormat = "General"

    End If

    Next

    Next

    End Sub

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments