INDIRECT & Dynamic Ranges - what am I missing?

tdhood

Board Regular
Joined
Jul 25, 2005
Messages
118
Hello, All -

I've just encountered some wierdness that I can't get past. I've got a spreadsheet with several dozen dynamic ranges - defined in the "Define Name" dialog box using the INDIRECT function.

As an example, I've got a dynamic range TestRange1 defined as so:

TestRange1=INDIRECT(Sheet1!$A$1)

The value in A1 is the string "Sheet1!$A$2:$A$21" (without quotes)

Here's where the wierdness pops up:

The function =ROWS(TestRange1) evaluates successfully to 20;while the function =ROWS(INDIRECT("TestRange"&"1")) coughs up a #REF!.

Now, it gets stranger: If I explicity define the ranges (instead of dynamic definitions), I can use the INDIRECT function to access the range.

Anybody seen this? Got a workaround?

tdh
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
The function =ROWS(TestRange1) evaluates successfully to 20;while the function =ROWS(INDIRECT("TestRange"&"1")) coughs up a #REF!.

All I can suggest is that Indirect() being a volotile function, doesn't like being nested. Here is a simpler breakdown of what you have done (I think) without the confusion of the Named Range.
Book1
ABCD
1Sheet1!c4
2
3sheet1!a1
410
510
6
7Sheet1!c4
810
9
10#REF!
Sheet1


Notice that the formula in A8 operates as expected, while A10 does not. I think difference is related to the structure of the Dependency Table. In one case, you could categorize the relationship as Cascaded, while the other is Nested. I think in your case, you are creating a Nested relationship, which can be problematic with Nested functions which are also Volatile (as Indirect is).

I think the most reasonable advice I can give you is related the patient who says "Doctor, it hurts when I do THIS."

Don' do it.

That being said, what are you trying to accomplish... perhaps we can come up with an alternative...
 
Upvote 0
INDIRECT in:

=ROWS(INDIRECT("TestRange"&"1"))

expects a string that can be resolved as a reference.

While TestRange1, which is INDIRECT(Sheet1!$A$1), resolves to an array object.
 
Upvote 0
And yet, this works:
Book1
ABCD
1sheet1!c1:c5
2
3Sheet1!a1
4
55
6
7sheet1!c1:c5
85
9
105
Sheet1


Which also gives lie to my initial evaluation... I think I am even more confused now.
 
Upvote 0
INDIRECT in:

=ROWS(INDIRECT("TestRange"&"1"))

expects a string that can be resolved as a reference.

While TestRange1, which is INDIRECT(Sheet1!$A$1), resolves to an array object.
~Aladin

Aladin,

Can you elaborate? (Or link to where you have already elaborated if you can?) To what "array" object is this resolving to? I have bumped my head on this kind of thing myself and I can never quite suss out why it fails.

edit: Hiya, Paul. Trust you've been well.
 
Upvote 0
INDIRECT in:

=ROWS(INDIRECT("TestRange"&"1"))

expects a string that can be resolved as a reference.

While TestRange1, which is INDIRECT(Sheet1!$A$1), resolves to an array object.
~Aladin

Aladin,

Can you elaborate? (Or link to where you have already elaborated if you can?) To what "array" object is this resolving to? I have bumped my head on this kind of thing myself and I can never quite suss out why it fails.

edit: Hiya, Paul. Trust you've been well.

Let A2:A5 on Sheet1 house:

2
3
2
5

Let A1 on Sheet1 house:

Sheet1!$A$2:$A$5

and define TestRange1 by means of:

[1]

=INDIRECT(Sheet1!$A$1)

F9 applied on [1] yields an array object, that is:

={2;3;2;5}

whose items are not strings, each of which would evaluate just to itself: 2 ==> 2, etc.

[2]

=INDIRECT(TestRange1)

==> #REF!,

because TestTange1 evaluates to ={2;3;2;5}, whose items INDIRECT cannot resolve to a refernce.

[3]

=TestRange1

which is the same thing as [1] and will evaluate to: ={2;3;2;5}.

[4a]

=INDIRECT("TestRange1")

or

[4b]

=INDIRECT("TestRange"&1)

would both evaluate to #REF! for TestRange1 presumably ==> {2;3;2;5}, whose items are unresolvable.
 
Upvote 0
Hi tdhood:

Unless I am missimng something here, for me on EXCEL2000 on Windows XP, both of the following worked ...

=ROWS(TestRange1)

as well as

=ROWS(INDIRECT("TestRange"&"1"))

In the following illustration I have used the above two and several other combinations ...
Book1
ABCDEF
1Sheet4!$A$2:$A$21=ROWS(TestRange1)=ROWS(INDIRECT("TestRange"&"1"))=ROWS(INDIRECT("TestRange1"))=ROWS(INDIRECT("TestRange"&1))=ROWS(INDIRECT(A$1))
212020202020
32
43
54
65
76
87
98
109
1110
1211
1312
1413
1514
1615
1716
1817
1918
2019
2120
Sheet4
 
Upvote 0
Unless I am missimng something here, for me on EXCEL2000 on Windows XP, both of the following worked ...

=ROWS(TestRange1)

as well as

=ROWS(INDIRECT("TestRange"&"1"))

In the following illustration I have used the above two and several other combinations ...

Yogi -

In your example, are you explicity defining "TestRange1" in the "Define Name" box? If so, then yes; it will behave properly. My difficulty comes when I try to dynamically define "TestRange1" in the "Define Name" box, using another INDIRECT statement.

Try re-working your example using =INDIRECT(Sheet4!$A$1) to define "TestRange1" & see if any of your formulas blow up.

tdh
 
Upvote 0
Aladin,

As always, much obliged for the lesson.<sup>1</sup> (y) This just got added to my list of favorites to use for future reference.<hr /><sup>1</sup>(I've lost count of how many times I've said this.)
 
Upvote 0
....
Yogi -

In your example, are you explicity defining "TestRange1" in the "Define Name" box? If so, then yes; it will behave properly. My difficulty comes when I try to dynamically define "TestRange1" in the "Define Name" box, using another INDIRECT statement.

Try re-working your example using =INDIRECT(Sheet4!$A$1) to define "TestRange1" & see if any of your formulas blow up.

tdh
Thanks for the clarification tdhood. Let us have another look ...
Book3
ABCDE
1$A$2:$A$21following is my work around to be able to use
21=INDIRECT("TestRange"&1) as a dynamic range
32
43I named cell A1 as TestRange1 (instead of =INDIRECT($A$1) then
54=INDIRECT("TestRange"&1) resulted in$A$2:$A$21
65=INDIRECT(INDIRECT("TestRange"&1)) resulted in5
76and =ROWS(INDIRECT(INDIRECT("TestRange"&1))) resulted in20
87
98
109
1110
1211
1312
1413
1514
1615
1716
1817
1918
2019
2120
22
Sheet1
 
Upvote 0

Forum statistics

Threads
1,214,386
Messages
6,119,215
Members
448,874
Latest member
b1step2far

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top