How to turn text into a number using VBA?

marlonsaveri

Board Regular
Joined
Jan 28, 2011
Messages
68
This program runs through the worksheets in search of some values. To find them, it puts on a table to build a chart.
The following macro pastes a value in the table in the correct region.

The sub receives a sheet (which contains the table), a value that will be inserted in it and receives an index i refers to the worksheet where that value was taken.

The problem is:
Not always the number (variable what) is a number. Sometimes it can be a number between brackets or two numbers separated by hyphens.

If two numbers separated by hyphens, I use "Split" and print on two lines.
If a number in brackets, I remove them by "replace", but there is a problem: the number is saved as text, so the program can not build the chart.
How to turn text into a number using VBA? Or how to solve this problem?

Code:
Sub PasteOnTable(mysheet As Worksheet, what As String, i As Integer)
    Dim LastRow As Integer
    LastRow = Cells(Rows.count, 1).End(xlUp).Row 
    LastRow = LastRow + 1 
    
    If IsNumeric(what) Then 
        mysheet.Cells(LastRow, 1) = Sheets(i).Name 
        mysheet.Cells(LastRow, 2) = what
    Else
        correct = Replace(what, "[", "") 
        correct = Replace(what, "]", "")
        If IsNumeric(correct) Then 
            mysheet.Cells(LastRow, 1) = Sheets(i).Name 
            mysheet.Cells(LastRow, 2).FormulaR1C1 = correct
        Else 
            vet = Split(what, "-")
            mysheet.Cells(LastRow, 1) = Sheets(i).Name 
            mysheet.Cells(LastRow, 2) = vet(0) 
            mysheet.Cells(LastRow + 1, 1) = Sheets(i).Name 
            mysheet.Cells(LastRow + 1, 2) = vet(1) 
        End If
    End If
    
    mysheet.Range("B2", Cells(LastRow, 2)).NumberFormat = "0.00" 'it doesn't solve.
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try

Rich (BB code):
Sub PasteOnTable(mysheet As Worksheet, what As String, i As Integer)
    Dim LastRow As Integer
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    LastRow = LastRow + 1
    
    If IsNumeric(what) Then
        mysheet.Cells(LastRow, 1) = Sheets(i).Name
        mysheet.Cells(LastRow, 2) = what
    Else
        correct = Replace(what, "[", "")
        correct = Replace(what, "]", "")
        If IsNumeric(correct) Then
            mysheet.Cells(LastRow, 1) = Sheets(i).Name
            mysheet.Cells(LastRow, 2).Value = Val(correct)
        Else
            vet = Split(what, "-")
            mysheet.Cells(LastRow, 1) = Sheets(i).Name
            mysheet.Cells(LastRow, 2) = Val(vet(0))
            mysheet.Cells(LastRow + 1, 1) = Sheets(i).Name
            mysheet.Cells(LastRow + 1, 2) = Val(vet(1))
        End If
    End If
    
    mysheet.Range("B2", Cells(LastRow, 2)).NumberFormat = "0.00" 'it doesn't solve.
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,773
Messages
6,126,822
Members
449,340
Latest member
hpm23

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