INDIRECT - Summing a range through sheets - Does not seem to be possible?

  • Thread starter Thread starter Alan
  • Start date Start date
A

Alan

Hi All,

I am trying to sum a range through a set of sheets (monthly reports) using a
basic formula as follows:

=SUM('20030731:20030831'!E7)

This works fine by summing E7 in each of the sheets from 20030731 to
20030831 inclusive (in this particular case it is just those two sheets).

However, when I try to construct that range using INDIRECT it does not seem
to work:

=SUM(INDIRECT("'20030731:20030831'!E7"))


Is this at all possible?

Thanks,

Alan.
 
Alan said:
However, when I try to construct that range using INDIRECT it does not seem
to work:

=SUM(INDIRECT("'20030731:20030831'!E7"))


Is this at all possible?

This is not possible because indirect is not among the functions that can be
used with 3-D references (reference ranges that span multiple worksheets or
workbooks).

The only functions that can be used with 3-D referenced are:

SUM(), AVERAGE(), AVERAGEA(), COUNT(), COUNTA(), MAX(), MAXA(), MIN(),
MINA(), PRODUCT(), STDEV(), STDEVA(), STDEVP(), STDEVPA(), VAR(), VARA(),
VARP(), and VARPA().

Regards
 
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

"Alan" <[email protected] <mailto:[email protected] in message
<

Hi All,

I am trying to sum a range through a set of sheets (monthly reports) using a
basic formula as follows:

=SUM('20030731:20030831'!E7)

This works fine by summing E7 in each of the sheets from 20030731 to
20030831 inclusive (in this particular case it is just those two sheets).

However, when I try to construct that range using INDIRECT it does not seem
to work:

=SUM(INDIRECT("'20030731:20030831'!E7"))


Is this at all possible?

Thanks,

Alan.

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

"Ridimz" <[email protected] <mailto:[email protected] in
message <

This is not possible because indirect is not among the functions that can be
used with 3-D references (reference ranges that span multiple worksheets or
workbooks).

The only functions that can be used with 3-D referenced are:

SUM(), AVERAGE(), AVERAGEA(), COUNT(), COUNTA(), MAX(), MAXA(), MIN(),
MINA(), PRODUCT(), STDEV(), STDEVA(), STDEVP(), STDEVPA(), VAR(), VARA(),
VARP(), and VARPA().

Regards

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Hi Ridimz,

Isn't the relavent function (in respect of the 3-D limits) the SUM formula?

Surely the INDIRECT function will simply return a range reference (which is
3-D in thiscase), which is then passed to SUM for evaluation?

I investigated further:

I entered the range formula in a cell:

{=('20030731:20030831'!E7)}

If you then evaluate this (highlight and press F9) it does not return an
array.

This makes the behaviour of the INDIRECT / SUM interaction entirely
understandable, but how does SUM correctly interpret the range (array) under
normal circumstances and sum the individual elements?

Thanks,

Alan.
 
Alan said:
Hi Ridimz,

Isn't the relavent function (in respect of the 3-D limits) the SUM
formula?

No! In your example SUM is meaningless, you simply used it to return
whatever INDIRECT gives to it (in this case garbage). You then attempted to
use INDIRECT to gather information from multiple sheets, which will fail
because it is incapable of gathering information from 3-D dereferences.

Surely the INDIRECT function will simply return a range reference (which is
3-D in thiscase), which is then passed to SUM for evaluation?

'20030731:20030831'!E7 is an invalid cell reference unless used with one of
the functions I identified earlier. If used with INDIRECT, you will get a
#REF error. SUM(#REF) = #REF. If you place the same reference inside SUM it
works just fine. Conclusion: INDIRECT will not return 3-D range references
(it does not understand them).
I investigated further:

I entered the range formula in a cell:

{=('20030731:20030831'!E7)}

If you then evaluate this (highlight and press F9) it does not return an
array.

This makes the behaviour of the INDIRECT / SUM interaction entirely
understandable, but how does SUM correctly interpret the range (array) under
normal circumstances and sum the individual elements?

The internal workings of the SUM function is unknown to me, however, it
(along with the other functions I named early) is one of the few functions
that understand 3-D references.

Regards,
Ridimz
 
....

First, set your newsreader to quote properly.
Surely the INDIRECT function will simply return a range reference (which is
3-D in thiscase), which is then passed to SUM for evaluation?

You're making an unfounded assumption. In Excel, no matter what you may
prefer to believe, ranges are *EXCLUSIVELY* 2-D, i.e., they must fit within
a single worksheet. Complicating matters, Range objects can be collections
of single area (rectangular blocks of cells) ranges, but they must all be in
the same worksheet.

This is one of those things you have to learn empirically. Typical for
Microsoft, XL2K (at least) online help includes 3-D 'ranges' in those things
that could be stored in Range object. Good luck trying to construct one in
VBA! Can't be done. Also, Range objects have no properties that reflect the
third (worksheet) dimension.

So, Range objects can only be 2-D, so 3-D referents can't be Range objects.
Since INDIRECT can only return references to Range objects or #REF! errors,
INDIRECT can't return 3-D references.
I entered the range formula in a cell:

{=('20030731:20030831'!E7)}

If you then evaluate this (highlight and press F9) it does not return an
array.

No, because Excel provides no true support for 'worksheet-axis' *ordered*
dimensionality. [Digression: there's one and only one exception to this of
which I'm aware - the NPV function accepts 3-D references as it's second or
subsequent argument, and it does iterate through 3-D references in a given
order. This is another instance in which online help is (to be charitable)
incomplete.] That is, Excel provides no means to convert 3-D references into
anything at could be entered into worksheet cells. The only thing Excel can
do/does with 3-D references is iterate through all cells in them in a
*limited* number of functions that return scalar (single value) results,
such as SUM, AVERAGE, etc. [NPV can return arrays, but only if it's first
argument, interest rate(s), is an array.]
This makes the behaviour of the INDIRECT / SUM interaction entirely
understandable, but how does SUM correctly interpret the range (array) under
normal circumstances and sum the individual elements?

Because 3-D references are *neither* ranges *nor* arrays. 3-D references are
syntactic bandages Microsoft introduced in Excel 4 in response to Lotus
introducing TRUE 3-D functionality in 123 Release 3 back in 1989. True to
Microsoft form, they didn't want to invest much in actual development and
programming, so they didn't incorporate true 3-D functionality into Excel.
Instead, they created a new syntactic entity that functioned somewhat like
data consolidation, and you should note that 3-D references can only be used
in data consolidation-like functions.

Much has been written about this in the microsoft.public.excel.* and
comp.apps.spreadsheets newsgroups over the years. Check the Google Groups
archive if you're curious. Aside from that, get used to the fact that Excel
can't do what you want it to do *except* by using user-defined functions.
Something like


Function si3d(tul As Variant, blr As Variant) As Variant
Dim ra As String, si As Long, i As Long, wsc As Sheets

On Error Resume Next

If TypeName(tul) = "String" Then Set tul = Range(tul)
If Err.Number <> 0 Then
si3d = CVErr(xlErrRef)
Exit Function
End If

If TypeName(blr) = "String" Then Set blr = Range(blr)
If Err.Number <> 0 Then
si3d = CVErr(xlErrRef)
Exit Function
End If

On Error GoTo 0

If Not tul.Parent.Parent Is blr.Parent.Parent Then
si3d = CVErr(xlErrRef)
Exit Function
End If

'the + 0.5 prevents si from being 0
si = Sgn(blr.Parent.Index - tul.Parent.Index + 0.5)

ra = Range(tul.Address(0, 0), blr.Address(0, 0)).Address(0, 0)

Set wsc = tul.Parent.Parent.Worksheets

For i = tul.Parent.Index To blr.Parent.Index Step si
si3d = si3d + Application.Sum(wsc(i).Range(ra))
Next i
End Function


In your example formula, use this udf as

=si3d(INDIRECT("'20030731'!E7"),"'20030831'!E7")

The two arguments to si3d are taken to be either range references to or
string range addresses of the possibly 2-D ranges containing opposite
corners of the 3-D reference over which to sum all cell values.
 
Hi Harlan,
First, set your newsreader to quote properly.

Hopefully now fixed. I have also been advised to use OE and something
called Quotefix - I'll investigate that too.
Surely the INDIRECT function will simply return a range reference (which is
3-D in thiscase), which is then passed to SUM for evaluation?

You're making an unfounded assumption. In Excel, no matter what you may
prefer to believe, ranges are *EXCLUSIVELY* 2-D, i.e., they must fit within
a single worksheet. Complicating matters, Range objects can be collections
of single area (rectangular blocks of cells) ranges, but they must all be in
the same worksheet.

This is one of those things you have to learn empirically. Typical for
Microsoft, XL2K (at least) online help includes 3-D 'ranges' in those things
that could be stored in Range object. Good luck trying to construct one in
VBA! Can't be done. Also, Range objects have no properties that reflect the
third (worksheet) dimension.

So, Range objects can only be 2-D, so 3-D referents can't be Range objects.
Since INDIRECT can only return references to Range objects or #REF! errors,
INDIRECT can't return 3-D references.
I entered the range formula in a cell:

{=('20030731:20030831'!E7)}

If you then evaluate this (highlight and press F9) it does not return an
array.

No, because Excel provides no true support for 'worksheet-axis' *ordered*
dimensionality. [Digression: there's one and only one exception to this of
which I'm aware - the NPV function accepts 3-D references as it's second or
subsequent argument, and it does iterate through 3-D references in a given
order. This is another instance in which online help is (to be charitable)
incomplete.] That is, Excel provides no means to convert 3-D references into
anything at could be entered into worksheet cells. The only thing Excel can
do/does with 3-D references is iterate through all cells in them in a
*limited* number of functions that return scalar (single value) results,
such as SUM, AVERAGE, etc. [NPV can return arrays, but only if it's first
argument, interest rate(s), is an array.]
This makes the behaviour of the INDIRECT / SUM interaction entirely
understandable, but how does SUM correctly interpret the range (array) under
normal circumstances and sum the individual elements?

Because 3-D references are *neither* ranges *nor* arrays. 3-D references are
syntactic bandages Microsoft introduced in Excel 4 in response to Lotus
introducing TRUE 3-D functionality in 123 Release 3 back in 1989. True to
Microsoft form, they didn't want to invest much in actual development and
programming, so they didn't incorporate true 3-D functionality into Excel.
Instead, they created a new syntactic entity that functioned somewhat like
data consolidation, and you should note that 3-D references can only be used
in data consolidation-like functions.

Much has been written about this in the microsoft.public.excel.* and
comp.apps.spreadsheets newsgroups over the years. Check the Google Groups
archive if you're curious. Aside from that, get used to the fact that Excel
can't do what you want it to do *except* by using user-defined functions.
Something like


Function si3d(tul As Variant, blr As Variant) As Variant
Dim ra As String, si As Long, i As Long, wsc As Sheets

On Error Resume Next

If TypeName(tul) = "String" Then Set tul = Range(tul)
If Err.Number <> 0 Then
si3d = CVErr(xlErrRef)
Exit Function
End If

If TypeName(blr) = "String" Then Set blr = Range(blr)
If Err.Number <> 0 Then
si3d = CVErr(xlErrRef)
Exit Function
End If

On Error GoTo 0

If Not tul.Parent.Parent Is blr.Parent.Parent Then
si3d = CVErr(xlErrRef)
Exit Function
End If

'the + 0.5 prevents si from being 0
si = Sgn(blr.Parent.Index - tul.Parent.Index + 0.5)

ra = Range(tul.Address(0, 0), blr.Address(0, 0)).Address(0, 0)

Set wsc = tul.Parent.Parent.Worksheets

For i = tul.Parent.Index To blr.Parent.Index Step si
si3d = si3d + Application.Sum(wsc(i).Range(ra))
Next i
End Function


In your example formula, use this udf as

=si3d(INDIRECT("'20030731'!E7"),"'20030831'!E7")

The two arguments to si3d are taken to be either range references to or
string range addresses of the possibly 2-D ranges containing opposite
corners of the 3-D reference over which to sum all cell values.

A truly outstanding reply to my question.

Thank you very much indeed.

As a rule I prefer to avoid UDFs, but given your holistic notes above on "3D
ranges" I will make an exception in this case.

Unless you indicate that you prefer I don't, I will also be using your notes
above in the model to explain the use of the UDF for those that may follow
me - with appropriate acknowledgement and a link to here of course.

Regards,

Alan.
 
Why do you want to put this within INDIRECT? The only reason that
immediately comes to my mind is that you need to add the contents of a
series of sheets where the date based series is changable.

If that's the case then this file might be a help:
http://www.bygsoftware.com/examples/zipfiles/consol.zip
It's in the "Accountants" section on page:
http://www.bygsoftware.com/examples/examples.htm

The "Bread-Roll" consolidation method - great for accountants. See how
simple it is to consolidate any combination of your organisation's accounts.
(No VBA used)


--

Regards
Andy Wiggins
www.BygSoftware.com
Home of "Save and BackUp",
"The Excel Auditor" and "Byg Tools for VBA"
 
Andy Wiggins said:
Why do you want to put this within INDIRECT? The only reason that
immediately comes to my mind is that you need to add the contents of a
series of sheets where the date based series is changable.

If that's the case then this file might be a help:
http://www.bygsoftware.com/examples/zipfiles/consol.zip
It's in the "Accountants" section on page:
http://www.bygsoftware.com/examples/examples.htm

The "Bread-Roll" consolidation method - great for accountants. See how
simple it is to consolidate any combination of your organisation's accounts.
(No VBA used)

Hi Andy,

Thanks for the link - interesting stuff in there.

However, I would prefer not to have to move sheets around to make this work.
The users of the model would definitely get confused with that kind of
thing, and I would forever be having to fix it up.

Good solution for more excel literate users perhaps.

Thanks,

Alan.
 
Much has been written about this in the microsoft.public.excel.* and
comp.apps.spreadsheets newsgroups over the years. Check the Google Groups
archive if you're curious. Aside from that, get used to the fact that Excel
can't do what you want it to do *except* by using user-defined functions.
Something like


Function si3d(tul As Variant, blr As Variant) As Variant
Dim ra As String, si As Long, i As Long, wsc As Sheets

On Error Resume Next

If TypeName(tul) = "String" Then Set tul = Range(tul)
If Err.Number <> 0 Then
si3d = CVErr(xlErrRef)
Exit Function
End If

If TypeName(blr) = "String" Then Set blr = Range(blr)
If Err.Number <> 0 Then
si3d = CVErr(xlErrRef)
Exit Function
End If

On Error GoTo 0

If Not tul.Parent.Parent Is blr.Parent.Parent Then
si3d = CVErr(xlErrRef)
Exit Function
End If

'the + 0.5 prevents si from being 0
si = Sgn(blr.Parent.Index - tul.Parent.Index + 0.5)

ra = Range(tul.Address(0, 0), blr.Address(0, 0)).Address(0, 0)

Set wsc = tul.Parent.Parent.Worksheets

For i = tul.Parent.Index To blr.Parent.Index Step si
si3d = si3d + Application.Sum(wsc(i).Range(ra))
Next i
End Function


In your example formula, use this udf as

=si3d(INDIRECT("'20030731'!E7"),"'20030831'!E7")

The two arguments to si3d are taken to be either range references to or
string range addresses of the possibly 2-D ranges containing opposite
corners of the 3-D reference over which to sum all cell values.

Hi,

I tried this function, and it works fine until I save and close and then
re-open the workbook.

When I first set it up, I can reference the function from a worksheet using
the normal method:

=Sum3D(.....

Note: I changed the formula name to Sum3D - not a contributing factor I
hope?

However, once I save and then re-open the workbook, the function is no
longer available, and the previously working functions return #NAME?

If I then try to reference the function from the listings (Insert -
Function - Select 'User Defined') then the function is listed under the
Workbook_Name.Sum3D.Sum3D

This is as though the function is in another workbook, when it is not.

If I then try to use the function by referencing it *as if* in another book,
it does then work.

Does anyone have any idea why that should be happening?

Thanks,

Alan.
 
Alan said:
I tried this function, and it works fine until I close and then
re-open the workbook.

When I first set it up, I can reference the function from a
worksheet using the normal methods.

However, once I save and then re-open the workbook, the function is no
longer available, and the previously working functions return #NAME?

If I then try to reference the function from the listings (Insert -
Function - Select 'User Defined') then the function is listed under the
workbook name.Module1.Si3D

This is as though the function is in another workbook, when it
obviously is not.

If I then try to use the function by referencing it as if in another book,
it does then work.

Does anyone have any idea why that should be happening?

Thanks,

Alan.

Please ignore this post - it was supposed to have been deleted before I sent
it, but I mucked up.

Apologies.

Please refer to this posting as a replacement:


Thanks,

Alan.
 
Andy Wiggins said:
More literate Excel users!

This is the most basic version I use for consolidation, on client sites,
because it uses standard Excel functionality.
....

Most experienced spreadsheet users use a variation on it from time to time.
It's efficient for static periods, but it's not flexible, so a pain for
arbitrary periods. UDFs for summing 3D variable references are nothing more
than 3D analogs of summing over 2D variable references. If INDIRECT provides
functionality that can't be provided in any other way in 2D cases (and given
the frequency of responses using INDIRECT, a solid prima facie case could be
made that this is so), couldn't there also be cases where some 3D analog to
INDIRECT would also be useful?

What you suggest would be adequate for the case the OP included in his
original post. However, OPs often give oversimplified examples. Even if the
worksheets involved were unchanging so that bracketting worksheet would
never need to move, if the cell addresses varied, your approach couldn't
cope.
 
More literate Excel users!

This is the most basic version I use for consolidation, on client
sites, because it uses standard Excel functionality.

--

Regards
Andy Wiggins
www.BygSoftware.com
Home of "Save and BackUp",
"The Excel Auditor" and "Byg Tools for VBA"


Hi Andy,

Perhaps you are right! It would make me nervous about future support
requirements though.

However, for now, I am pursuing the solution that Harlan outlined elsewhere
within this thread.

However, I am having trouble with the UDF appearing as if it was in a
totally different workbook when it is not.

See my post here:


Any ideas?

If so, perhaps best to reply to that post rather than this one to keep the
discussion easier to follow.

Thanks for your continuing support!

Alan.
 
Alan said:
I tried this function, and it works fine until I save and close and then
re-open the workbook.

When I first set it up, I can reference the function from a worksheet using
the normal method:

=Sum3D(..... ....
However, once I save and then re-open the workbook, the function is no
longer available, and the previously working functions return #NAME?
....

You must have put this in a general VBA module, but did you put this
function in one and only one workbook? If not, and it appears in several
files, that could cause problems.

I've only ever had problems like what you're describing when there are
multiple udfs with the same name in several open files and/or modules.
 
Back
Top