如何使用 WorksheetFunction.VLookup 错误处理 1004 错误? [英] How to error handle 1004 Error with WorksheetFunction.VLookup?

查看:32
本文介绍了如何使用 WorksheetFunction.VLookup 错误处理 1004 错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

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

这篇关于如何使用 WorksheetFunction.VLookup 错误处理 1004 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆