我有以下代码,当在Vlookup公式中发现错误时,我无法跳到下一个交互(i)。它给出了错误“编译错误子或函数未定义”
例如,如果Vlookup公式在第二次交互中给出错误(i = 2),则代码应跳过下一行代码并转到下一次交互(i = 3)
Observable
答案 0 :(得分:1)
我会使用On Error Resume Next
:
For i = 1 To 10:
If Range("A" & i).Value = "" Then
Else
On Error Resume Next
datavalue = Application.WorksheetFunction.VLookup(Sheets("DataManipulation").Range("A" & i), Sheets("ExtractQ1").Range("A1:G1048576"), 4, False)
If Err = 0 Then
Sheets("DataManipulation").Range("B" & i).Value = datavalue
End If
On Error Goto 0 ' Reactivate normal error reporting
End If
Next i
答案 1 :(得分:1)
您可以利用Application.VLookUp并检查其返回值是否为错误:
dim dataValue As Variant
For i = 1 To 10
If Range("A" & i).Value <> "" Then
dataValue = Application.VLookup(Sheets("DataManipulation").Range("A" & i), Sheets("ExtractQ1").Range("A1:G1048576"), 4, False)
If Not IsError(dataValue) Then Sheets("DataManipulation").Range("B" & i).Value = dataValue
End If
Next i