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. HansV 462.7K Reputation points MVP Volunteer Moderator
    2012-03-27T08:08:07+00:00

    Try this macro (test on a copy of the workbook first):

    Sub ConvertText()

        Dim wsh As Worksheet

        Dim rng As Range

        Dim cel As Range

        Application.ScreenUpdating = False

        For Each wsh In Worksheets

            Set rng = Nothing

            On Error Resume Next

            Set rng = wsh.Cells.SpecialCells(xlCellTypeConstants, xlTextValues)

            On Error GoTo 0

            If Not rng Is Nothing Then

                For Each cel In rng

                    cel.Value = cel.Value

                Next cel

            End If

        Next wsh

        Application.ScreenUpdating = True

    End Sub

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2012-03-27T07:57:55+00:00

    Thanks Daniel, this is working but it will be too manual and time consuming to convert line by line worksheet by worksheet, because my workbook has more than 50 worksheet.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2012-03-27T07:56:04+00:00

    Hi Tasosk,

    It will bring all the Text to zero. In fact in just want to convert '0116 to 116.

    Best Regards,

    Lawrence

    Was this answer helpful?

    0 comments No comments