+ Reply to Thread
Results 1 to 5 of 5

Formula (or VBA Macro) to add cells over dynamic Worksheets

  1. #1
    Registered User
    Join Date
    10-19-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Formula (or VBA Macro) to add cells over dynamic Worksheets

    Hi Everyone,

    I am normally pretty good about researching and finding answers on my own, but I am on a time crunch here and can't figure this one out.

    I have a Workbook that will contain a variable number of worksheets labeled 1-# (where # can be between 28-31) for the days in a given month.

    I need to develop either a formula or macro that will add the numerical values in specific cells across a user defined list of those worksheets.

    For example, Let us assume that the cell I wish to have summed is A1 and the user wants to see the summation of worksheets 5, 6, 7, 8, and 9.

    I have a cell in a different worksheet that allows the user to select the first date/worksheet (5 in this case) and one that allows the user to select the last date/worksheet (9 in this case). These are cells E6 and E7 respectivley. I know that I can use =INDIRECT("'"&E6&"'!A1") to return the value in the cell A1 of the first worksheet (5!A1) and I know that I can use a 3-D formula to sum A1 accross multiple worksheets: =SUM(5:9!A1).

    What I cant figure out is how to 'nest' these two formulas together. I have tried:

    =SUM(INDIRECT("'"&E6&":"&E7&"'!A1)) and
    =SUM(INDIRECT("'"&E6:E7&"'!A1)) and
    =SUM(INDIRECT("'"&E6&:&E7&"'!A1))

    I have also tried =SUM(INDIRECT('E6:E7'!A1)) which returns a #REF! error and it (curriously) changes the formula to =SUM(INDIRECT('E6:[E7]E7'!A1)). I have no idea why it is adding the [E7] into this formula.

    Any help would be great.

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Formula (or VBA Macro) to add cells over dynamic Worksheets

    It would go a lot better with a sample workbook.
    Attach a sample workbook (not a picture or pasted copy). Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Formula (or VBA Macro) to add cells over dynamic Worksheets

    You could try this

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    where E1 and E2 hold the first and last sheets.

  4. #4
    Registered User
    Join Date
    10-19-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Formula (or VBA Macro) to add cells over dynamic Worksheets

    Quote Originally Posted by Bob Phillips View Post
    You could try this

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    where E1 and E2 hold the first and last sheets.
    This worked perfectly!

    Would it be too much to ask you to explain how/why this works? That way I have the understanding of the work and not just the temporary fix?

    Mainly I am not 100% sure why there are two "indirect" functions and why it uses sumproduct. Also, I dont know why row is used at all.

    I think that the SUMIF and the "<>" argument tells it only to sum cells that are not blank, and the second indirect appears to be similar to what I was trying to do; but the rest has me stumped.

    Anyway, thanks again!

  5. #5
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Formula (or VBA Macro) to add cells over dynamic Worksheets

    Quote Originally Posted by GingerBeast View Post

    Would it be too much to ask you to explain how/why this works? That way I have the understanding of the work and not just the temporary fix?

    Mainly I am not 100% sure why there are two "indirect" functions and why it uses sumproduct. Also, I dont know why row is used at all.
    One of the INDIRECTs, INDIRECT(E1&":"E2") is used within the ROW function to build an array of the numeric sheet names as defined within E1 and E2. Then that array is appended with "!A1" to build an array of the cells to be queried in each of the target sheets, 5!A1, 6!A1, etc.

    Quote Originally Posted by GingerBeast View Post
    I think that the SUMIF and the "<>" argument tells it only to sum cells that are not blank, and the second indirect appears to be similar to what I was trying to do; but the rest has me stumped.
    You need a function to extract the values from each of the cells in that final array, which SUMPRODUCT then aggregates. I used SUMIF as I grabbed an old example of mine, and as you say used the <> as a non-blank criteria, but I could have, should have, used a simple SUM

    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Last edited by Bob Phillips; 06-14-2018 at 06:53 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 03-25-2016, 12:33 PM
  2. [SOLVED] Dynamic reference of cells in other worksheets
    By JamesFletcher in forum Excel General
    Replies: 2
    Last Post: 04-01-2014, 12:11 PM
  3. linking two cells between dynamic ranges and worksheets
    By dimattia in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-24-2013, 11:10 AM
  4. [SOLVED] Macro to copy formula to blank cells in a dynamic range
    By masben in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-25-2013, 07:07 AM
  5. Macro for printing dynamic range from multiple worksheets
    By dube.rajneesh in forum Excel General
    Replies: 1
    Last Post: 09-19-2012, 09:22 AM
  6. Replies: 2
    Last Post: 01-30-2011, 11:38 AM
  7. Replies: 4
    Last Post: 02-04-2010, 12:48 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1