VBA Excel – IFERROR & VLOOKUP error

Using Application.WorksheetFunction.VLookup( will break the code if the value is not found.

Wrap it in vba error control.

Function CCC(A As Range)
Dim B As Variant
B = -1
On Error Resume Next
B = Application.WorksheetFunction.VLookup(A.Value, Sheets("DAP").Range("$B$4:$X$7"), 5, False)
On error goto 0
CCC = B

End Function

Another method is to Late Bind by removing the .WorksheetFormula and then testing the result.

Function CCC(A As Range)
Dim B As Variant
B = Application.VLookup(A.Value, Sheets("DAP").Range("$B$4:$X$7"), 5, False)
If IsError(B) Then
    CCC = -1
Else
    CCC = B
End If
End Function

Leave a Comment