I have a table in Excel with column headings that correspond to part of a dynamic named range elsewhere in my workbook. For example, I have these column headings: "10", "20", etc., and these dynamic named ranges: "ExampleRange10", "ExampleRange2", etc. I'd like to enter a VLookup formula that references ExampleRange10 by concatenating the string "ExampleRange" and the column heading "10". This would allow me to simply extend the formula across all columns in the table, instead of manually typing "ExampleRange10", "ExampleRange20", etc. in each column's formula.
I'm aware of the INDIRECT function, and have used it successfully in the past with named ranges, but it doesn't seem to be working with the dynamic named range in this case. I'm guessing this is a nuance that has something to do with how dynamic named ranges are defined by Excel (they don't show up in the named range dropdown to the left of the formula bar, and they have some interesting properties in VBA, for example). Is there a way I can use the INDIRECT formula in conjunction with a dynamic named range, or is there another way that I can go about solving this problem?
Edit: Here are the exact formulas used.
This is the main formula: =VLOOKUP(B2,INDIRECT("ExampleRange"&C1),2,FALSE)
where C1 contains "10" and the formula for my dynamic named range called "ExampleRange10" is: =OFFSET(Sheet1!$F$2,0,0,COUNTA(Sheet1!$F$2:$F$25),2)
. The main formula returns "#REF!", but it works correctly when I remove the dynamic named range formula and simply define "ExampleRange10" as a static range.
I hit this exact brick wall recently and the answer as you have already guessed is simply that you can't reference dynamic named ranges with INDIRECT.
You can however use the dynamic range formula itself as INDIRECT's argument, but this is no use for what you want to do. Somewhat of a PITA since it's the kind of functionality that would be very useful.
Today I was tinkering with Excel named ranges, and I discovered that, while it is true that you cannot compute the name of the range in the
INDIRECT()
call itself, you can still get it in a pure "Excel-way" by adding an intermediate step: just create some hidden cell in which you compute the named range.For example, say that
A1
contains the "dynamic part" of the range name, then inA2
use the formula= "ExampleRange" & A1
, and now you have the full range name, which you can use as= INDIRECT(A2)
.As best I could tell after doing further research, Excel's
INDIRECT
function simply doesn't work with dynamic ranges. There might be a clever way to get around usingINDIRECT
and sticking to the non-VBA Excel world, but I'm unaware of such a way. Instead, I ended up creating a user-defined function very similar to the one described here. I altered my main formula to read=VLOOKUP(B2,DINDIRECT("ExampleRange"&C1),2,FALSE)
, whereDINDIRECT
is the name of the VBA function I created.The only downsides (which may or may not be downsides depending on how you look at it) to this alternative is that the workbook must be saved as a macro-enabled workbook and the use of a custom function isn't very self-documenting and requires a little explanation to other users. All things considered, though, this was an acceptable solution for me.
For the link-averse, here's the code:
Note: Although this solution worked, I'm not going to accept my answer because I don't want to discourage others from posting better solutions. Also, I'm new to the site, so sorry if I'm breaking any etiquette codes by answering my own question...I just thought I'd share the exact solution that I used in case others find it useful.
Untested, but I think this would work:
user defined function to return the address of your dynamically named range:
then you should be able to use your vlookup formula:
I know this is quite old, but I only just came across this and thought I'd add a solution that avoids any VBA coding in case it helps anyone else who stumbles across this:
This is assuming the naming convention being 10,20,30,etc and will not be ideal for hundreds of ranges.
If your data has headers like 10, 20 etc., then you don't need to use Indirect. Why not just use Index/Match to select the data you need?
Name you whole table ExampleRanges for example and use this formula: