Vlookup表错误

时间:2017-04-24 06:24:54

标签: excel vba excel-vba 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

1 个答案:

答案 0 :(得分:0)

尝试以下代码(不使用SelectActiveCell):

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