Vlookup财产不工作在VBA

我正在寻找一个variablesselrow它保存在一个表中的string值。 有时会显示错误

无法获得工作表函数类的vlookup属性

我也把error handling代码,但仍然发生错误。

这发生错误

for i=1 to last_row 'code selrow= range("D"&i) opening_inventory_today = 0 On Error Resume Next opening_inventory_today = Application.WorksheetFunction.VLookup(selrow, ActiveSheet.Range("A4:B" & lr), 2, False) 

使用On Error Resume Next不处理的错误,你需要陷阱的情况, selrow无法findselrow ,然后引发错误信息(或任何你想要的)。

试试下面的代码:

 If Not IsError(Application.VLookup(selrow, ActiveSheet.Range("A4:B" & lr), 2, False)) Then opening_inventory_today = Application.VLookup(selrow, ActiveSheet.Range("A4:B" & lr), 2, False) ' rest of your code here Else MsgBox "VLookup was unable to find " & selrow, vbInformation End If