Dynamic Sum Across Sheets

davin1130

New Member
Joined
Mar 15, 2019
Messages
39
My workbook contain 13 sheets. One sheet for each month (January thru December) plus one sheet to be ale to run totals. I can easily use =Sum(January:December!B2) to calculate the sum of all B2 cells in sheets January thru December. However, i want the ability to select the range of sheets based off of cell A1 (which will have the desired beginning month) and cell A2 (which will have the desired ending month). Please Help !
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Will the range of sheets selected be in chronological order? For Example:
Jan-Mar
Sep-Dec
Jun-Dec

but not
Jan, Jun, Dec
May, Jul
Aug, Sep, Dec
 
Upvote 0
I have tried every which way to Sunday to create a normal formula to express the sum of all the values across a 3d Range. There seems to be some folks that created 3d dynamic ranges, but I couldn't get that to work. INDIRECT won't work with 3D ranges. So I created a User Defined Function (UDF) that can be used in a cell formula. Paste that into a standard VBA module (ie, module1, module2). In a cell type in this formula:
=SumRange3D(B4)
* Where B4 is the range in all the months that you want to sum.
* Copy down and across as needed.
* UDF's should be used sparingly because they tend to take longer to calculate then standard Excel Functions. A few hundred won't make that much of a difference


Code:
'Provide current cell to Rng
Function SumRange3D(Rng As Range) As Double
  Dim BegMonth As Integer
  Dim EndMonth As Integer
  Dim X As Long
  Dim MonthName As String
  Dim Tot As Double
  Dim RngAddr As String
  
  Application.Volatile
  
  BegMonth = Range("A1").Value      'First month as a number
  EndMonth = Range("B1").Value      'Last Month as a number
  RngAddr = Rng.Address             'Address of current cell
  
  For X = BegMonth To EndMonth
    MonthName = Format(DateSerial(2019, X, 1), "mmmm") 'Full Month Name
    Tot = Tot + Sheets(MonthName).Range(RngAddr).Value  'Add to total
  Next X
  SumRange3D = Tot
    
End Function
 
Upvote 0
I forgot to tell you that cell A1 needs to be a number expressing the first month in the series and Cell B1 needs to be the last month. You could change the ranges for these and change the macro to look at the new addresses.
 
Last edited:
Upvote 0
Hello,

As an alternative to the UDF, in order to have a Dynamic 3D Sum of selected Sheets, you can use an Array Formula ...

1. Create a Named range : Sheets in order not to list all the Sheets names ...
in the Refers To =SUBSTITUTE(GET.WORKBOOK(1),"["&GET.WORKBOOK(16)&"]","")

2. Then use cell B3 to type in your ' From Sheet ' --- in cell B4 type in your ' To Sheet ' and in cell B5 your Target Cell or range

3. The following Array Formula will generate the Sum you are looking for
Code:
=SUM(IF((MATCH(Sheets,Sheets,0)>=MATCH($B$3,Sheets,0))*(MATCH(Sheets,Sheets,0)<=MATCH($B$4,Sheets,0)),N(INDIRECT("'"&Sheets&"'!"&$B$5))))

Hope this will help
 
Upvote 0
James,

What exactly is the named range formula returning? Is it just returning sheets? I have Sheets January through December and they are Worksheets 19 through 30 respectively. Do I need to change the GET.Workbook(1) to Get.Workbook(19) and Get.Workbook(16) to Get.Workbook(30)?

Or

Get.Workbook(1) does not return a sheet name.

OR

Get.Workbook(1) returns the sheet names with the workbook name and SUBSTITUTE removes the workbook name

Jeff
 
Upvote 0
Still trying to help out the OP by evaluating the formulas given by James

I got the Named range created and the formula below works.

I replaced the end part of the formula to include a cell reference rather than a single cell. If one were trying to use this to get the sum of many rows and columns from the 3D range, we would have one issue.

Let's say that the Month tables all have a range of B4:J100. And I want my Totals to also show up in the same range on the Summary sheet. When I try to use the CELL("address",B4) instead of a hard coded B4, it complains that there is circular reference. Strange, because I'm not creating my totals on any of the month pages. The Sheets named range must be confusing the formula to think that all sheets are being evaluated.

To solve this, we would have to put the Totals formulas in different columns then values on the Month Sheets. The formula below could be in M4; it would return the sum of all the Month values in cell B4.

=SUM(IF((MATCH(Sheets,Sheets,0)>=MATCH($A$3,Sheets,0))*(MATCH(Sheets,Sheets,0)<=MATCH($A$4,Sheets,0)),N(INDIRECT("'"&Sheets&"'!"&CELL("address",B4)))))

on my summary sheet, I put the beginning month in cell A3 and the End Month in cell A4.

Jeff
 
Last edited:
Upvote 0
THANK YOU FOR YOU HELP THUS FAR !!!!
I'm still running into problems though ... i created a named range and named it "Sheets", putting the formula =Substitute(get.workbook(1),"["&get.workbook(16)&"]","") into the "Refers to" field AS IS ... the cell i'm using to put the formula in is Q8 and i modified the formula changing $A$3 to $A$1, $A$4 to $A$2, and B4 to B2 ... Cell B2 in all sheets has a value of 1 so i'd expect my totals value to be 3 since i'm looking at sheets January/February/March .... i'm getting a value of zero using both formulas. Any ideas what i'm doing wrong ??
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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