Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA code showing error 2042 for reason unsure

Tags:

excel

vba

I have a variable fila with a full line with Excel's values

The problem is when in Excel I have #N/A, VBA takes that value like Error 2042.

I cannot assign that value to valor and produce an error. Until this point everything is ok, now I am trying to define a On Error Goto to go to the next iteration in the For loop, but I do not know why VBA doesn't handle the error.

Do While Not IsEmpty(ActiveCell)

    txt = ActiveCell.Value2
    cell = ActiveCell.Offset(0, 1).Value2       
    fila = Range("C20:F20")

    For j = 1 To UBound(fila, 2)
        On Error GoTo Siguiente
        If Not IsEmpty(fila(1, j)) Then            
            valor = fila(1, j)
            cmd = Cells(1, j + 2).Value2
            devolver = function1(cmd, txt, cell, valor)
            arrayDevolver(p) = devolver
            p = p + 1                
        End If
Siguiente:
    Next
Loop 
like image 623
Jonathan Raul Tapia Lopez Avatar asked Dec 07 '12 10:12

Jonathan Raul Tapia Lopez


1 Answers

This is not a VBA error - so error handling won't catch it. To detect this issue use IsError before writing to your array

The simple code below handles the Error 2042 problem with this test

Sub Test()
    Dim varIn As Variant
    [a1].FormulaR1C1 = "=NA()"
    If IsError([a1].Value2) Then
        varIn = "skip record"
    Else
        varIn = [a1].Value2
    End If
End Sub
like image 138
brettdj Avatar answered Sep 23 '22 17:09

brettdj