Vlookup表错误
我试图循环E列的值,以便我可以使用它VLookup
在另一个工作表上,并返回值列F.
它一直给我的错误
应用程序定义或对象定义的错误
在线上:
result = Application.WorksheetFunction.VLookup(look, Sheet2.Range("B:H"), 2, False)
我的代码
Dim X As Integer Dim look As Variant Dim result As Variant X = 2 Sheet3.Range("E2").Select Do Until IsEmpty(ActiveCell) look = Sheet3.Cells(X, 5).Value ActiveCell.Offset(1, 0).Select result = Application.WorksheetFunction.VLookup(look, Sheet2.Range("B:H"), 2, False) Sheet3.Cells(X, 6).Value = result X = X + 1 Loop
尝试下面的代码(不使用Select
和ActiveCell
):
Option Explicit Sub VLookupHandleNA() Dim X As Long Dim Look As Variant Dim Result As Variant X = 2 With Sheet3 Do Until IsEmpty(.Range("E" & X).Value) Look = .Range("E" & X).Value Result = Application.VLookup(Look, Sheet2.Range("B:H"), 2, False) If Not IsError(Result) Then .Range("F" & X).Value = Result Else ' if Vlookup returns an error, just write something in the cell to let the user know .Range("F" & X).Value = "VLookup wasn't able to find " & Look End If X = X + 1 Loop End With End Sub