如何错误处理1004 WorksheetFunction.VLookup错误? [英] How to error handle 1004 Error with WorksheetFunction.VLookup?
本文介绍了如何错误处理1004 WorksheetFunction.VLookup错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码:
Dim wsFunc As WorksheetFunction: Set wsFunc = Application.WorksheetFunction
Dim ws As Worksheet: Set ws = Sheets("2012")
Dim rngLook As Range: Set rngLook = ws.Range("A:M")
'within a loop
currName = "Example"
cellNum = wsFunc.VLookup(currName, rngLook, 13, False)
VLookup不会总是找到结果;但是当它没有找到结果时,线错误出来之前我甚至可以错误检查下一行。
VLookup is not expected to always find a result; but when it does not find a result the line errors out before I can even error check it the next line.
错误:
运行时错误'1004':无法获取WorksheetFunction类的VLookup属性
Run-time error '1004': Unable to get the VLookup property of the WorksheetFunction class
当找到结果时,它工作正常。在这里处理错误的好方法是什么?
It works fine when a result is found. What's a good way to handle errors here?
推荐答案
有一种方法可以跳过代码中的错误,继续循环,希望它有帮助:
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
这篇关于如何错误处理1004 WorksheetFunction.VLookup错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文