You can't use a SUM
across multiple worksheets: the entirety of each range has to be within a single worksheet. SUM('A>:<A'!A1)
is an invalid formula — you would need to use SUM('A>'!A1,'1'!A1,'2'!A1,'<A'!A1)
to get the result you want. (i.e. specifying multiple ranges)
INDIRECT
and CHOOSE
will ignore things like Sheet Order, so can't be used for this either.
You might be able to work something out with VBA, using Worksheet.Index
references, but not with normal Excel formulae. Here is a User Defined Function (put it in a new Module in VBA, not in the Worksheet!) that should do what you are after:
Option Explicit
Option Private Module
Public Function SUM3D(reference1 As Range, reference2 As Range) As Variant
Application.Volatile
SUM3D = CVErr(xlErrNA) 'Default to an Error
On Error GoTo FunctionError
Dim SumRange As String, TotalSum As Double, TempSum As Variant, StartIndex As Long, EndIndex As Long, CurrIndex As Long
TotalSum = 0
TempSum = 0
'Create the 2D range bounded by the 2 references, which will extend through the sheets
SumRange = Worksheets(1).Range(Worksheets(1).Range(reference1.Address(True, True, xlA1, False)), _
Worksheets(1).Range(reference2.Address(True, True, xlA1, False)) _
).Address(True, True, xlA1, False)
StartIndex = reference1.Worksheet.Index
EndIndex = reference2.Worksheet.Index
If StartIndex = EndIndex Then
'Same sheet, no need to make it 3D
SUM3D = Application.Sum(reference1, reference2)
Else
'Different sheets, so cycle through them.
For CurrIndex = StartIndex To EndIndex Step Sgn(EndIndex - StartIndex)
'Ignore Charts, DialogSheets, MacroSheets, etc
If Sheets(CurrIndex).Type = xlWorksheet Then
TempSum = Application.Sum(Sheets(CurrIndex).Range(SumRange))
If IsError(TempSum) Then
SUM3D = TempSum 'Return the Error
Exit Function
Else
TotalSum = TotalSum + TempSum 'Add this sheet's sum to the total
TempSum = 0
End If
End If
Next CurrIndex
End If
SUM3D = TotalSum
FunctionError:
On Error GoTo -1 'Clear error handler
End Function