简体   繁体   中英

Dynamic range for vlookup in Excel VBA

I am looking for a possibility to set a dynamic range. The problem is that Excel does not accept the typing. Five rows of a matrix should be looked through for each a. The vlookup is working with a fixed range such as "E43:AT47" but not for a combined one.

        Range_Zeile_Start = 5 * a - 12
        Range_Zeile_End = 5 * a - 8
        Range_Count = "R" & Range_Zeile_Start & "C5:R" & Range_Zeile_End & "C46"

        Fahrzeit_kk = Application.WorksheetFunction.VLookup(Cells(1, b).Value, Range_Count, 42, False)

You need to set-up your Range correct.

See code below:

Dim VlookRng As Range

Range_Zeile_Start = 5 * a - 12
Range_Zeile_End = 5 * a - 8

' set up the Range for the Vlookup
Set VlookRng = Range(Cells(Range_Zeile_Start, 5), Cells(Range_Zeile_End, 46))
Fahrzeit_kk = Application.WorksheetFunction.VLookup(Cells(1, b).value, VlookRng, 42, False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM