在VBA中没有范围的Vlookup

时间:2015-05-13 13:40:02

标签: vba excel-vba vlookup excel

寻找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

1 个答案:

答案 0 :(得分:0)

如果您有一个要使用的范围内的单元格(在示例中类似于G2)您可以使用Range(&#34; G2&#34;)。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