2017-04-24 59 views
1

我试图环路E列的值,这样我可以用它来VLookup另一个工作表和值返回给列FVLOOKUP表错误

它一直给我的错误

的应用程序定义或上线对象定义的错误

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 
+0

避免使用选择http://stackoverflow.com/q/10714251/4539709-现在“B:H”上有任何值吗? – 0m3r

+0

是的“B:H”上有数值 –

回答

0

尝试下面的代码(不使用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 
+0

现在有用了,谢谢你的帮助! –