VBA Excel - IFERROR & VLOOKUP error

后端 未结 1 530
名媛妹妹
名媛妹妹 2020-12-21 20:35

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

相关标签:
1条回答
  • 2020-12-21 21:03

    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
    
    0 讨论(0)
提交回复
热议问题