【发布时间】:2020-02-12 19:06:10
【问题描述】:
如果您能在下面的代码上帮助我,我将不胜感激。我正在尝试使用 VBA 查找函数进行查找,当查找的值在表数组中不可用时,它会给出错误 1004。我尝试使用“On resume Resume Next”命令在出现错误时跳过,但不是跳过并给出空白结果,而是在其上抛出以前的值。
Sub x()
Dim d As String
Set src = Range("A1:A5") 'list of names needs to be searched
Set Rng = Range("D1:E5") 'table array
On Error Resume Next
For Each cell In src
d = Application.WorksheetFunction.VLookup(cell.Value, Rng, 2, 0)
cell.Offset(0, 1).Value = d
Next cell
End Sub
【问题讨论】:
-
这个
On Error Resume Next只是隐藏了错误消息,但错误仍然存在。它不能解决任何问题,它只是告诉 VBA “如果有错误,请不要显示任何人”。以后不要在没有完整错误处理的情况下使用该行!您可能会从阅读VBA Error Handling – A Complete Guide 中受益