0

我正在尝试设置 VB,以便可以对动态范围的值进行 VLOOKUP。它总是从同一个地方开始,但它可能会根据 H4 中的任何值进一步向下结束。

编辑:这是代码,它可以工作。

谢谢亚历克斯!

Public Sub State()

    Dim refRng As Range, ref As Range, dataRng As Range
    Dim i As Variant
    Dim count As Integer
    i = Sheet2.Range("H1").Value
    i = i + 3 'offset of when to start

    Set refRng = Sheet2.Range("D8:" & Cells(8, i).Address) '//horizontal range of look up values
    Set dataRng = Sheet13.Range("A:C") '//data block you want to look up value in

    For Each ref In refRng
    ref.Offset(1, 0) = Application.WorksheetFunction.VLookup(refRng, dataRng, 2, True)
    Next ref
End Sub
4

1 回答 1

0

这可能会有所帮助:

Sub LookUp()
    Dim refRng As Range, ref As Range, dataRng As Range

    Set refRng = Worksheets(1).Range("D8:F8") //horizontal range of look up values
    Set dataRng = Worksheets(2).Range("A1:B4") //data block you want to look up value in

    For Each ref In refRng
        ref.Offset(1, 0) = WorksheetFunction.VLookup(ref, dataRng, 2, 0)
    Next ref
End Sub

您可以在此处设置对查找值和要查询的数据的引用。然后只需遍历水平范围内的值并查找该值。

于 2013-08-27T18:55:29.933 回答