簡體   English   中英

Vlookup在VBA中沒有范圍

[英]Vlookup without a range in 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

如果您有一個要使用的范圍內的單元格(例如您的示例中的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM