Referencing Dynamic Named Range in Excel Formula

岁酱吖の 提交于 2019-11-30 20:07:36

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.

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.

Cool Blue

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:

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

Untested, but I think this would work:

user defined function to return the address of your dynamically named range:

Function Named_Range_Address(Range_Name As Range, _ 
    Optional SheetName As Boolean) As String 

    Dim strName As String 
    Application.Volatile 

    If SheetName = True Then 
        strName = "'" & Range_Name.Parent.Name & "'!" & Range_Name.Address 
    Else 
        strName = Range_Name.Address 
    End If 

    Named_Range_Address = strName 
End Function 

then you should be able to use your vlookup formula:

=VLOOKUP(B2,INDIRECT(named_range_address("ExampleRange"&C1,TRUE)),2,FALSE)
Dave

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.

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 in A2 use the formula = "ExampleRange" & A1, and now you have the full range name, which you can use as = INDIRECT(A2).

Adding a new twist, it is possible to use a named range with the Address and Indirect functions. I have a case where I am setting named ranges for a series of tables and am using the following:

Named Range: WWDH-FF-PI which points to Linear!$A$19 (first cell in table)

to get the address: $T$56: =ADDRESS(MATCH(S56,Linear!A:A,0),1,1,1,"Linear")

Then using the offset function copied multiple times to create a pivot table:

=OFFSET(INDIRECT($T$56),C5,$T$57-1)

So, the Address function can be embedded (or wrapped) into the Indirect function to create a dynamic cell address.

DavePenn

I know this is a really old thread, but I had the same issue, so perhaps my solution can help people in the future.

Basically, I created a Macro that would delete and re-define the range upon save, and give it a name. Therefore, the INDIRECT function would work as the range was not dynamic. All you need to do is save the workbook after adding any values to the named ranges

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Dim a, b, c, d, e, f As Integer
Dim data As Worksheet

Set data = ThisWorkbook.Worksheets("Data")

a = data.Range("A" & Rows.count).End(xlUp).row
b = data.Range("B" & Rows.count).End(xlUp).row
c = data.Range("C" & Rows.count).End(xlUp).row
d = data.Range("D" & Rows.count).End(xlUp).row
e = data.Range("E" & Rows.count).End(xlUp).row
f = data.Range("F" & Rows.count).End(xlUp).row



ActiveWorkbook.Names("KP").Delete
ActiveWorkbook.Names("KPT").Delete
ActiveWorkbook.Names("AP").Delete
ActiveWorkbook.Names("APT").Delete
ActiveWorkbook.Names("DISC").Delete
ActiveWorkbook.Names("SEATS").Delete

ActiveWorkbook.Names.Add Name:="KP", RefersTo:="=Data!$A$2:$A$" & a
ActiveWorkbook.Names.Add Name:="KPT", RefersTo:="=Data!$B$2:$B$" & b
ActiveWorkbook.Names.Add Name:="AP", RefersTo:="=Data!$C$2:$C$" & c
ActiveWorkbook.Names.Add Name:="APT", RefersTo:="=Data!$D$2:$D$" & d
ActiveWorkbook.Names.Add Name:="DISC", RefersTo:="=Data!$E$2:$E$" & e
ActiveWorkbook.Names.Add Name:="SEATS", RefersTo:="=Data!$F$2:$F$" & f

End Sub
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!