How to error handle 1004 Error with WorksheetFunction.VLookup?

前端 未结 3 491
不思量自难忘°
不思量自难忘° 2020-11-22 06:24

I have this code:

Dim wsFunc As WorksheetFunction: Set wsFunc = Application.WorksheetFunction
Dim ws As Worksheet: Set ws = Sheets(\"2012\")
Dim rngLook As R         


        
3条回答
  •  余生分开走
    2020-11-22 06:59

    There is a way to skip the errors inside the code and go on with the loop anyway, hope it helps:

    Sub new1()
    
    Dim wsFunc As WorksheetFunction: Set wsFunc = Application.WorksheetFunction
    Dim ws As Worksheet: Set ws = Sheets(1)
    Dim rngLook As Range: Set rngLook = ws.Range("A:M")
    
    currName = "Example"
    On Error Resume Next ''if error, the code will go on anyway
    cellNum = wsFunc.VLookup(currName, rngLook, 13, 0)
    
    If Err.Number <> 0 Then
    ''error appeared
        MsgBox "currName not found" ''optional, no need to do anything
    End If
    
    On Error GoTo 0 ''no error, coming back to default conditions
    
    End Sub
    

提交回复
热议问题