0

I have an issue. I have values in a workbook that are getting read into an array. The values come from an XML list so sometimes can be numbers or text, and if they are numbers in text format ("1" for example), they need to be converted to number format, so I multiply them by 1. If the value is text "LS" for example, I am trying to use an error handler to keep the value as "LS".

I have developed the code below: It works once, but the next time I use a similar method (with 'Dummy2') the code it produces a 'Type Mismatch' error.

    On Error GoTo Dummy1
    For i = 1 To nrows1 - 1
        If (Worksheets("Import").Cells(i + 1, 26) * 1 = "") Then
Dummy1:

            Table1(i, 1) = Worksheets("Import").Cells(i + 1, 26)
        Else
            Table1(i, 1) = Worksheets("Import").Cells(i + 1, 26) * 1
        End If
    Next
    On Error GoTo 0

I also tried Clearing Err after the above code with no success.

Please help!

2 Answers2

1

You can use the IsNumeric function:

Sub test3()
Dim Test As Variant
Dim i As Long

For i = 1 To nrows1 - 1
Test = Worksheets("Import").Cells(i + 1, 26)
    If IsNumeric(Test) Then
        Table1(i, 1) = Test * 1
    Else
        Table1(i, 1) = Test
    End If
Next
End Sub
Doug Glancy
  • 27,214
  • 6
  • 67
  • 115
0

use VAL(Worksheets("Import").Cells(i + 1, 26)) to determine if it is a Number or not. VAL will give you 0 for "LS".

You could try TryParse functions - but not sure if its available in your version of VBA

Grantly
  • 2,546
  • 2
  • 21
  • 31