2017-04-24 58 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

現在有用了,謝謝你的幫助! –