2015-05-13 76 views
0

寻找Vlookup直到最后一行。有没有办法做到不给范围?它可以做到最后一排。在VBA中没有范围的Vlookup

Do While IsEmpty(Cells(i, Name)) = False 
     Str = Cells(i, Name) 

     ws2.Select 

     On Error Resume Next 
     Corp = Application.WorksheetFunction.VLookup(Str, ws2.Range("G2:S3000"), 4, False) 
     result = Application.WorksheetFunction.VLookup(Str, ws2.Range("G2:S3000"), 13, False) 
     'Band = Application.WorksheetFunction.VLookup(Str, ws2.Range("G2:S2500"), 7, False) 

     If (Err <> 0) Then 
     Else 
      ws1.Select 
      If (result <> 0) Then 
       Cells(i, 10) = result 
      End If 
      Cells(i, 9) = Corp 
     End If 

     i = i + 1 
     ws1.Select 
    Loop 
End Sub 

回答

0

如果您想要使用的范围内有一个单元格(如您的示例中的G2)可以使用Range(“G2”)。CurrentRegion。这将为您提供该单元所属的完整范围。 这个工作对我来说:

Public Sub GetLookupValue() 
    Dim rng As Range 
    Dim str As String 
    str = "c" 
    Set rng = Blad1.Range("G2").CurrentRegion 
    Debug.Print Application.WorksheetFunction.VLookup(str, rng, 2, False) 
    Set rng = Nothing 
End Sub 
+0

修改了范围(G2 “)。它不工作 – Butterfly

+0

上的错误继续下一步 公司= Application.WorksheetFunction.VLookup(力量,ws2.Range(” G2" ) ,4,False) result = Application.WorksheetFunction.VLookup(Str,ws2.Range(“G2”),13,False) – Butterfly

+0

想要得到一个答案吗?有没有办法看到这一行的结束代码 – Butterfly