Referencing Dynamic Named Range in Excel Formula

50,402

Solution 1

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 using INDIRECT 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), where DINDIRECT 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:

Public Function DINDIRECT(sName As String) As Range
     Dim nName As Name

     On Error Resume Next
          Set nName = ActiveWorkbook.Names(sName)
          Set nName = ActiveSheet.Names(sName)
     On Error GoTo 0

     If Not nName Is Nothing Then
          Set DINDIRECT = nName.RefersToRange
     Else
          DINDIRECT = CVErr(xlErrName)
End Function

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.

Solution 2

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.

Solution 3

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 your whole table ExampleRanges for example and use this formula:

Index(ExampleRanges, match(B2, index(ExampleRanges, , 1), 0), match(C1, index(ExampleRanges, 1,), 0))

Solution 4

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:

=VLOOKUP(B2,CHOOSE(C1/10,example10,example20,example30,example40),2,0)

This is assuming the naming convention being 10,20,30,etc and will not be ideal for hundreds of ranges.

Share:
50,402
Kyle Wurtz
Author by

Kyle Wurtz

actuary, designer, developer, photographer, philosopher, and overall nerd

Updated on July 28, 2022

Comments

  • Kyle Wurtz
    Kyle Wurtz almost 2 years

    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.

  • Kyle Wurtz
    Kyle Wurtz over 11 years
    Thanks for confirming that INDIRECT doesn't work with dynamic named ranges. I wholeheartedly agree that this functionality would be useful, and it's especially frustrating given how simple the workaround code is (so it should be very easy for them to incorporate it) and how often the problem comes up (at least for me).
  • Kyle Wurtz
    Kyle Wurtz over 11 years
    Thanks for the suggestion. I saw a function like this somewhere when I was googling, but this particular function requires the first argument to be of type Range, but since I'm concatenating the strings "ExampleRange" and the text in C1, I get an argument of type String. However, your suggestion to use a user-defined function is right on. I did find another function that takes the RangeName As String here, and that worked fine for me.
  • blackworx
    blackworx over 11 years
    Exactly. It's doubly frustrating when you have to come up with a solution that doesn't use UDFs (or VBA in any way) as I am often required to do.
  • blackworx
    blackworx over 8 years
    That works, but unfortunately the problem occurs with dynamic named ranges, i.e. a named range which refers to a variable number of cells, usually depending on their contents.
  • blackworx
    blackworx over 8 years
    It's not so much the name itself being dynamic that's the problem, it's when the range to which it refers is dynamic.
  • cyberponk
    cyberponk about 4 years
    Worked! Using Index solved my problem with Indirect in named formula :)