简体   繁体   中英

Excel VBA - referring to named range in ActiveCell.Formula within match lookup array

I'm trying to refer to a named range within the lookup array part of the match function within an active cell formula. It needs to be a named range (to the best of my understanding) because I'm going to later run the code over multiple tabs and on each tab the amount of cells that contain data to form the lookup array in column AJ2 will differ in size and the match function doesn't work if you run it over a column that also contains empty cells. This is my code as it currently stands and I'm getting the "Method formula of object range failed" error. It's definitely the match bit that's causing it because when I go into the cell and change that manually to refer directly to the cells in the array it works fine, but I can't do this as it needs to be futureproofed to run over multiple tabs and the lookup array length changes on each:

Sheets("1").Select
Dim rangeh As Range
Set rangeh = Range("AJ2")
Set rangeh = Range(rangeh, rangeh.End(xlDown))

Sheets("1").Select
Range("R2").Select
ActiveCell.Formula = "=VLOOKUP(INDIRECT(""AJ""&(MATCH(Q2, & rangeh & ,-1)+1)),AI:AK,3,FALSE)"

It also has to be an ActiveCell.Formula because R2 is itself going to be dragged down on each tab to a different place depending on the length of data in the adjacent column Q.

Any help appreciated. Thanks!

The -1 used as the [match_type] parameter in the MATCH() concerns me because it shouldn't be used on non-sorted data but you've mentioned that there may be blank cells. Specifically, using -1 as the [match_type] assumes the data is sorted in descending order. Need a little clarification on the exact nature of the data in column AJ.

I also think you are doubling up by using the INDIRECT() when constructing the VLOOKUP() with string concatenation within VBA may be better. I guessed at some points but here's what I came up with.

Dim rangeh As Range, rngQ As Range, w As Long, vWSs As Variant
vWSs = Array("1","2")
For w = LBound(vWSs) To UBound(vWSs)
    With Sheets(vWSs(w))
        Set rngQ = .Cells(2, 17).Resize(.Cells(Rows.Count, 17).End(xlUp).Row - 1, 1)
        Set rangeh = .Range(.Range("AJ2"), .Range("AJ2").End(xlDown))
        .Cells(2, 18).Formula = _
          "=VLOOKUP(INDIRECT(""AJ""&(MATCH(Q2," & rangeh.Address & ",-1)+1)),AI:AK,3,FALSE)"
        .Cells(2, 18).Resize(rngQ.Rows.Count, 1).FillDown
    End With
Next w
Set rangeh = Nothing
Set rngQ = Nothing

BTW, there should actually a worksheet literally named 1. If 1 was intended to be the worksheet index number, it should not be in quotes.

I've corrected some code, added .Address to rangeh in MATCH() and a loop to go through worksheets named 1 and 2. You should be able to add worksheet names to the Array(...) to increase the loop.

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