我有一個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)應該是一個很好的起點。 –