I\'m trying to create the equivalent of the =IFERROR(VLOOKUP(),-1) formula in excel VBA where the function would look up text in my data table and return the number in the 5
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