我有一个do until循环,它在另一个工作表中查找匹配,并在给定单元格与条件匹配时返回另一个工作表中的值。VBA - 使用Vlookup在循环中执行错误处理
代码工作除了当标准匹配的完全没问题但在另一片没有查找匹配(这可能取决于如何我的数据是从原始资料收集所以它不打扰我发生。)
我如何建立一个错误处理程序,以便如果这种情况确实发生 - 即条件匹配但没有查找匹配 - 那么我的代码只是移动到下一行,即下一个j
该图片是我的代码
我有一个do until循环,它在另一个工作表中查找匹配,并在给定单元格与条件匹配时返回另一个工作表中的值。VBA - 使用Vlookup在循环中执行错误处理
代码工作除了当标准匹配的完全没问题但在另一片没有查找匹配(这可能取决于如何我的数据是从原始资料收集所以它不打扰我发生。)
我如何建立一个错误处理程序,以便如果这种情况确实发生 - 即条件匹配但没有查找匹配 - 那么我的代码只是移动到下一行,即下一个j
该图片是我的代码
,你可以这样做:
On Error Goto ErrorHandler
然后在错误处理程序:
ErrorHandler:
'Check the Err.Number value and handle it appropriately
'then do something like
Resume Next
Dim res As Variant '<--| declare a Variant variable to hold the result of VLookups
Do Until j = 26
If Cells(j, i-1).Value= 0 And ... Then
res = Application.VLookup(... first VlookUp...) '<--| "try" first lookup and store its result in 'res'
If Not IsError(res) Then '<--| if Vlookup were successful, go ahead
Sheets("Main").Range("C" & BlankRow2 + 1).FormulaR1C1 = Application.VLookup(... first VlookUp...)
Sheets("Main").Range("A" & BlankRow2 + 1).FormulaR1C1 = Application.VLookup(... second VlookUp...)
Sheets("Main").Range("B" & BlankRow2 + 1).FormulaR1C1 = Application.VLookup(... third VlookUp...)
End If
End If
j = j + 1
Loop
On error Resume Next
你也可以使用类似实施更具体的错误处理程序
请将您的代码作为文本发布,而不是图片。 – haindl
您粘贴截图时遇到的问题比直接从VBE粘贴代码更麻烦。 –
另外... [错误处理Docs.SO主题](http://stackoverflow.com/documentation/vba/3211/error-handling#t=201611081609371522506)应该是一个很好的起点。 –