+ Reply to Thread
Results 1 to 21 of 21

Conditionally summing across a varying number of worksheets

  1. #1
    Registered User
    Join Date
    10-23-2017
    Location
    NS, CA
    MS-Off Ver
    2016
    Posts
    17

    Conditionally summing across a varying number of worksheets

    I have a stock tracking excel sheet we use at work which tracks units sold, proceeds, basis, gain/loss, ending units etc.

    The sheet has a data tab in which all the key information about stocks is input, and the number of stocks information is put in for can vary from 1 to 180 (the largest number I have dealt with).
    I have a macro which sorts and formats my data into a format I can use on the individual stock tracking worksheets and another macro to let me make as many copies of the tracking worksheet as I have stocks on the data tab.

    I run into problems when I get to the Summary page though.

    The summary page lists all the individual stocks in separate rows with their key information for a given year.

    Ex

    Sheet # Stock Name Units Sold Proceeds Basis Gain/Loss

    Where the sheet # corresponds to the name of the worksheet that stocks transaction history is on. I use numbers and they are between two blank sheets called A and Z, so my sheet tabs at the bottom look like this:
    Summary A 1 2 3 4 5 6 Z Data

    And the numbers vary based on the number of stocks.


    Now to ensure that all the transactions are being picked up by the summary and the worksheet tracking the transactions there is a set of check lines on the summary sheet for each column which is where I am running into difficulty.
    The check needs to take the totals from all the below rows (which works) , and the totals of all the transactions on each worksheet which makes up the total reported in the row and make sure they match.
    ie: Totals From Below Gives a number for each coloumn
    Check Gives the sum of all of the transactions on all the worksheets which make up the total reported below in the rows
    Difference Totals from Below - Check

    My problem is the check line. I cannot figure out how to get it to sum the values across all the numbered worksheets (and it needs to work for a variable number of sheets being present).
    As an example of what the line needs to report here is the basis for the units sold total. Units sold is the SUMIFS(E:E,C:C."Sell",AJ:AJ,$A$3) where E,C,and AJ are on the sheets A:Z and $A$3 is on the summary sheet.

    I know I don't need to reference the whole column, just a set of cells slightly higher then what i usually find I need but for the sake of a solutions it is easier to write E:E or even just E then E1:E500.


    Any help would be greatly appreciated.

  2. #2
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    43,965

    Re: Conditionally summing across a varying number of worksheets

    You will need something like this:

    =SUMPRODUCT(SUMIFS(INDIRECT("'"&List&"'!Range_To_Sum"),INDIRECT("'"&List&"'!Criterion_Range"),Criterion))

    Where List is a dynamic named range conatining a list of your sheets. However, without a sample sheet, it is not easy to provide more specific help.

    So....

    Will you please attach a SMALL sample Excel workbook (10-20 rows of data is usually enough)? Please don't attach a picture of one (no-one will want to re-type all your stuff before starting).

    1. Make sure that your sample data are truly REPRESENTATIVE of your real data. The use of unrepresentative data is very frustrating and can lead to long delays in reaching a solution.

    2. Make sure that your desired solution is also shown (mock up the results manually).

    3. Make sure that all confidential information is removed first!!

    4. Try to avoid using merged cells. They cause lots of problems!

    Unfortunately the attachment icon doesn't work at the moment. So, to attach an Excel file you have to do the following: Just before posting, scroll down to Go Advanced and then scroll down to Manage Attachments. Now follow the instructions at the top of that screen.
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU.

    Temporary addition of accented to illustrate ongoing problem to the TT: L? fh?ile P?draig sona dhaoibh

  3. #3
    Registered User
    Join Date
    10-23-2017
    Location
    NS, CA
    MS-Off Ver
    2016
    Posts
    17

    Re: Conditionally summing across a varying number of worksheets

    Hi Glenn,

    I have sheet here with values which make sense in the appropriate cells but none of the underlying formulas, as I cannot share that work. My data is fictional but I have made it similar to the actual data I work with.

    Hopefully I have covered all of the above points you mentioned.

    Allison
    Attached Files Attached Files

  4. #4
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,522

    Re: Conditionally summing across a varying number of worksheets

    Based on Glenn's post try the following on the summary sheet:
    In cell C2:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

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

    Let us know if you have any questions.
    Consider taking the time to add to the reputation of everybody that has taken the time to respond to your query.

  5. #5
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    43,965

    Re: Conditionally summing across a varying number of worksheets

    I completely misunderstood your request.
    1. Delete sheets A& Z. Not needed.

    2. Create this named range called "Sheets" (CTRL-F3 to view):
    =TRANSPOSE(GET.WORKBOOK(1))&T(NOW())

    It generates an array of all the tabs in the file.

    3. Use this array formula in A5, copied down:
    =IFERROR(INDEX(MID(Sheets,FIND("]",Sheets)+1,255),SMALL(IF(MID(Sheets,FIND("]",Sheets)+1,255)<>MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,255),ROW(INDIRECT("$A$1:$A$"&COUNTA(MID(Sheets,FIND("]",Sheets)+1,255))))),ROW(1:1)),1),"")

    It returns a list worksheets in the file, except the sheet on which the formula is listed. So It runs: Data, 1, 2, 3, etc.


    4. Use this formula in C5 copied down:
    =IFERROR(SUMIFS(INDIRECT("'"&A6&"'!E4:E50"),INDIRECT("'"&$A6&"'!C4:C50"),"Sell",INDIRECT("'"&$A6&"'!J4:J50"),$A$3),"")

    4a. Use this formula to return the stock names:
    =IFERROR(INDIRECT("'"&A6&"'!B1"),"")

    5. Use this formula in D5, copied down:
    =IFERROR(SUMPRODUCT(SUMIFS(INDIRECT("'"&A6&"'!G4:G50"),INDIRECT("'"&A6&"'!C4:C50"),"Sell",INDIRECT("'"&A6&"'!J4:J50"),$A$3)),"")


    I have no idea what's going on in E, F & G....

    It would have been better to leave the formulae in....
    Attached Files Attached Files

  6. #6
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    43,965

    Re: Conditionally summing across a varying number of worksheets

    I forgot to add:

    Array Formulae are a little different from ordinary formulae in that they MUST be confirmed in the FIRST CELL ONLY by pressing CTRL+SHIFT+ENTER to activate the array, not just ENTER. After that, the array can be dragged down as normal, to cover the desired range.

    You will know the array is active when you see curly brackets { } - or "curly braces" for those of you in the USA, or "flower brackets" for those of you in India - appear around the outside of your formula. If you do not use CTRL+SHIFT+ENTER you will (almost always) get an error message or an incorrect answer. Press F2 on that cell and try again.

    Don't type the curly brackets yourself - it won't work...

  7. #7
    Registered User
    Join Date
    10-23-2017
    Location
    NS, CA
    MS-Off Ver
    2016
    Posts
    17

    Re: Conditionally summing across a varying number of worksheets

    Hi

    I did the one in cell C2 but changed all the A6:A8's to M3 (which has that range in it) as my actual workbook can go from A6:A186 and changing that reference in all the formulas would get tedious quickly everytime the workbook changed.
    I am however getting a #ref error and I am not sure why, I also got the same error before changing the references to M3.

    {=SUMPRODUCT(SUMIFS(INDIRECT(" ' "&M3&" '!E5:E500"),INDIRECT(" ' "&M3&" '!C5:C500"),"Sell",INDIRECT(" ' "&M3&" '!AJ5:AJ500"),$A$3))}

    This is what I put in any ideas where I went wrong?

    Also I do know about array formulas and used ctrl + shift+enter. I only come seeking help when I have tried every way I now of to approach it and google has failed me.

  8. #8
    Registered User
    Join Date
    10-23-2017
    Location
    NS, CA
    MS-Off Ver
    2016
    Posts
    17

    Re: Conditionally summing across a varying number of worksheets

    Hi Glenn,

    Sorry I missed your first longer post. Will that work even though I have other sheets in the workbook (Rawdata, Cdn Summary, US Summary, 8621, Tax Factors, etc)? I couldn't leave the formulas in this workbook as it is one of a set which helps give the small company I work for a competitive edge and the capacity to do a larger scale of work then we could otherwise.

  9. #9
    Registered User
    Join Date
    10-23-2017
    Location
    NS, CA
    MS-Off Ver
    2016
    Posts
    17

    Re: Conditionally summing across a varying number of worksheets

    oh and only need to summarize the numbered sheets, the other sheets are calculations done on the data to prep it for entry in software.

  10. #10
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,522

    Re: Conditionally summing across a varying number of worksheets

    Quote Originally Posted by AllisonNS View Post
    I did the one in cell C2 but changed all the A6:A8's to M3 (which has that range in it) as my actual workbook can go from A6:A186 and changing that reference in all the formulas would get tedious quickly everytime the workbook changed...
    What about changing the range of data on the summary sheet to a table set to needed size? The formulas may then be written using structured references which should expand and contract to match the table (however, please test with your data).
    The formula for C2 could be:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    The formula for D2 could be:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Note: these are regular formulas so they do not need the special Ctrl, Shift, Enter activation.
    Let us know if you have any questions.
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    10-23-2017
    Location
    NS, CA
    MS-Off Ver
    2016
    Posts
    17

    Re: Conditionally summing across a varying number of worksheets

    This is working great right now. Super touchy in the writing for the rest of the columns but when working showing the right results. Thanks so much! I am going to hold off marking this solved until I find out if my boss is happy with the result and requirement of having the summary sheet a table.

  12. #12
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,522

    Re: Conditionally summing across a varying number of worksheets

    You're Welcome and thank you for the feedback. If your boss approves, please mark the thread as 'Solved' using the thread tools link above your first post. Let us know if we can be of further assistance if the boss doesn't approve. I hope that you have a blessed day.

  13. #13
    Registered User
    Join Date
    10-23-2017
    Location
    NS, CA
    MS-Off Ver
    2016
    Posts
    17

    Re: Conditionally summing across a varying number of worksheets

    Hi guys,

    I ran into another related problem on the summary sheet columns. I really should have looked at all the formulas before I asked for help. There are 3 columns the above formula won't work for but since two are almost identical (a difference of a single number) it is really only the 2 problems.

    I have an ending units column in the table which gets its value using a vlookup formula and i have a highest balance column which uses a max/maxifs formula to get its value. I am not sure how to go about combining those functions with the sumproduct function to get a summary result for all tabs.

    I am attaching the copy of the workbook where you helped me get the sumproduct with sumifs function working and adding in an example vlookup and max/maxifs function to make it easier for you to see what is going on. The column references have been changed to make no sense from a financial stand point but the formulas use the exact format from my work spreadsheet as they are a little more complex then using a single sumif function.

    Thank you for any help you can give me.
    Attached Files Attached Files

  14. #14
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,522

    Re: Conditionally summing across a varying number of worksheets

    Try the following array entered formula* in G6 and down on the 'Summary' sheet:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    *Array entered formulas are activated by simultaneously pressing the Ctrl, Shift and Enter keys.
    To check, cell G2, use try following formula:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Looking at the values in F7:F8 it appears that the values come from column G on each individual stock sheet, in a manner that "Buy" dictates a negative value and "Sell" dictates a positive value. However, this doesn't seem to hold true for cell F6. It could be that I simply don't understand accounting, however I would prefer an explanation of what values are included in those cells before attempting to write a formula to fill F2.
    I don't know where the values for E6:E8 come from so I am not attempting a formula for E2.
    Let us know if you have any questions.
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    10-23-2017
    Location
    NS, CA
    MS-Off Ver
    2016
    Posts
    17

    Re: Conditionally summing across a varying number of worksheets

    You do have the understanding of what is going on there, however if you look closer the numbers are based only on transactions in the year in cell A3 on the summary tab. So in D6:F6 there are no values listed since there are no sales in 2016 on the corresponding tab and it only needs to be reported when there is a sale in the year. Anyway I do not need summary formulas for the proceeds, basis, or G/L columns, the original sumif help you gave worked great. I needed it for column g and h (so a summary of ending units and max) which have the formulas for how the values are pulled in them.

  16. #16
    Registered User
    Join Date
    10-23-2017
    Location
    NS, CA
    MS-Off Ver
    2016
    Posts
    17

    Re: Conditionally summing across a varying number of worksheets

    On another note, that array formula will not work for column g as the summary as if the year was listed as say 2014 and the information was entered until 2016 it would only give me the 2016 ending units amount. As well testing it on my actual workbook it was out about 700 units. The formulas originally entered in column G have been tested as working for over 200 different stock portfolios showing no errors, so I am hesitant to change them.

  17. #17
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,522

    Re: Conditionally summing across a varying number of worksheets

    I can see the values in G6:H8 when I open the file attached to post #13, however as soon as I press the 'Enable Editing' button they disappear, so I assume that there are some 2016 version functions in the formulas that populate those cells that are not present in the 2010 version.
    I feel that the check formula for cell G2 is valid as it comes up with the same number using a different method which, as described in this article, is a valid way of checking. However, if you want to get a check using the 2016 version methods I can ask if any of the contributors with 2016 versions on their computers can help.
    If you want a check in H2, how would it differ from the value in G2? I am asking because the values in H6:H8 are the same as those in G6:G8, and again the 2010 version that I am using can not evaluate the formulas used.

  18. #18
    Registered User
    Join Date
    10-23-2017
    Location
    NS, CA
    MS-Off Ver
    2016
    Posts
    17

    Re: Conditionally summing across a varying number of worksheets

    Sorry I didn't realize we were having a versioning issue. The formula going down column g is:
    I*FERROR(I*FNA(V*L*OOKUP(D*ATE($A$3,12,31),I*NDIRECT(A6&"!"&$A$10),9),0),0

    and the formula going down column h to calculate it is:
    I*FERROR(IF($A$3<Y*EAR(I*NDIRECT([@[Tab'#]]&"!"&$B$10)),0,I*FNA(M*AX(M*AXIFS(I*NDIRECT([@[Tab'#]]&"!"&$C$10),I*NDIRECT([@[Tab'#]]&"!"&$D$10),$A$3),[@[Ending Units]]),0)),0

    I tried to upload however I am getting blocked when I select go advanced. the hopefully compatible formulas for those columns are

    G I*FERROR(V*L*OOKUP(D*ATE($A$3,12,31),I*NDIRECT(A6&"!"&$A$10),9),0
    H {I*FERROR(I*F($A$3<Y*EAR(I*NDIRECT([@[Tab'#]]&"!"&$B$10)),0,M*AX(M*AX(I*F(I*NDIRECT([@[Tab'#]]&"!"&$D$10)=$A$3,I*NDIRECT([@[Tab'#]]&"!"&$C$10)),[@[Ending Units]]))),0}

    My summary problem is about summarizing the vlookup and maxifs functions so a solution for the two 2010 compatible formulas should hopefully work for the incompatible formulas as well?

    Thanks

  19. #19
    Registered User
    Join Date
    10-23-2017
    Location
    NS, CA
    MS-Off Ver
    2016
    Posts
    17

    Re: Conditionally summing across a varying number of worksheets

    Sorry about the asterisk's in the formulas, it was not letting me post a response

  20. #20
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Conditionally summing across a varying number of worksheets

    Hi. I'm late to the party.

    I don't have MAXIFS so I can't test at my end, but shouldn't the cell reference range in 'Summary' D10 be j4:j50 instead of a4:a50?

    a4:a50 are dates, column j has the years only which is what you are referencing in 'Summary' A3.

    INDIRECT([@[Tab'#]]&"!"&$D$10),$A$3
    Dave

  21. #21
    Registered User
    Join Date
    10-23-2017
    Location
    NS, CA
    MS-Off Ver
    2016
    Posts
    17

    Re: Conditionally summing across a varying number of worksheets

    Hi,

    Yes it should be. Sorry about that. I made a typo while transferring the formulas over to the example sheet i posted here from my actual sheet which has the year in column ae.

+ 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. Giant merge macro with varying number of worksheets
    By Knighterist in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-05-2014, 12:21 AM
  2. Summing every nth value in varying ranges of data
    By Taojones in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-20-2013, 04:03 PM
  3. [SOLVED] Code for summing data from a varying number of worksheets
    By niftysquirrel in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-14-2013, 11:04 AM
  4. [SOLVED] Conditionally summing two columns
    By Butcher1 in forum Excel General
    Replies: 8
    Last Post: 04-05-2012, 09:58 AM
  5. VBA help - summing a certain text-value in varying ranges
    By Flabbergaster in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-28-2010, 09:37 AM
  6. Summing values conditionally
    By dupaski in forum Excel General
    Replies: 2
    Last Post: 12-14-2008, 03:07 PM
  7. Summing Number of Worksheets
    By PipHarrison in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-11-2005, 09:54 AM

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