+ Reply to Thread
Results 1 to 20 of 20

Need a macro to sum or subtotal ranges of varying lengths in a column

  1. #1
    Forum Contributor
    Join Date
    06-25-2013
    Location
    Longview, TX
    MS-Off Ver
    MS365
    Posts
    338

    Need a macro to sum or subtotal ranges of varying lengths in a column

    I've reached a point in this macro where I'm stuck with NO clue how to achieve this, or even whether it can be done. In the Before sheet below, I need to bold the cells in H & I if the word "Total" is in G. I could probably manage that part, but here's the weird part: I need to add a formula to H & I of each row with "Total" in G to sum the numbers above it, from the previous total down to the current total. But there's no telling how long each range might be on a given report; from 1 row up to 8 rows.

    THEN, in the second row below the end of columns H & I, I need to put a formula totalling all of the preceding Totals

    I wonder if there might be a way to tell it to put a formula in H by each cell that has "Total" in G, with the formula summing or subtotalling everything from the cell in H that has data in F to the cell just above the "Total". Any ideas?

    I'm trying to attach a file; I hope it works!

    Thanks!

    Jenny
    Attached Files Attached Files
    Last edited by zookeepertx; 05-01-2014 at 11:56 AM.

  2. #2
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Need a macro to sum or subtotal ranges of varying lengths in a column

    With the code below to fill the date.

    Please Login or Register  to view this content.
    after that I made an table and after that a pivot table of the data.

    also a graph is added to the file.

    see the attached file.
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need a macro to sum or subtotal ranges of varying lengths in a column

    Maybe:

    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    06-25-2013
    Location
    Longview, TX
    MS-Off Ver
    MS365
    Posts
    338

    Re: Need a macro to sum or subtotal ranges of varying lengths in a column

    Hi Oeldere - Unfortunately I don't think that's going to work for this report. It needs to stay in the same format as it started. But thank you for your suggestion!

    Hi John - That's great! I'm fascinated by watching it step through! (I may be just a little too easy to amuse, LOL!)
    Just a couple of small things:
    I'd like to not have the dollar signs by the numbers. Can I just take out the part that says rcell.Style = "currency" or will that screw things up?
    Also, can we have the zeros stay in their cells or do they have to be replace by dashes?

    Thanks!

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need a macro to sum or subtotal ranges of varying lengths in a column

    Maybe:

    Please Login or Register  to view this content.
    Last edited by JOHN H. DAVIS; 05-01-2014 at 03:48 PM.

  6. #6
    Forum Contributor
    Join Date
    06-25-2013
    Location
    Longview, TX
    MS-Off Ver
    MS365
    Posts
    338

    Re: Need a macro to sum or subtotal ranges of varying lengths in a column

    Another question:

    This report has a lot of data after this Summary section that's at the top. This data is formatted completely different from the Summary section. So, in my larger macro that I want to add this to, I've set SL as the row that has "Grand Total" in column F, denoting the end of the Summary section. (I totally forgot to include the Grand Total line before. VERY sorry!) So, I changed the coding to this:
    Please Login or Register  to view this content.
    But that gives me the result in the After tab on the newly attached workbook.

    I also tried replacing all of the
    Please Login or Register  to view this content.
    with
    Please Login or Register  to view this content.
    to try to confine the activity of the macro to the Summary section. But that caused the Grand Total line to move up directly below the last Total line, with H & I containing some HUGE number more than twice what you'd get if you summed ALL the lines above it & then added another line right below that which sums all the data above (giving twice the total needed, so obviously THAT'S wrong!

    I'm going to be SO happy with this when it's done, but it sure is a process getting there, LOL!
    Thanks!

    Jenny
    Attached Files Attached Files
    Last edited by zookeepertx; 05-01-2014 at 05:49 PM.

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need a macro to sum or subtotal ranges of varying lengths in a column

    What does Grand Total represent?

  8. #8
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need a macro to sum or subtotal ranges of varying lengths in a column

    Try:

    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    06-25-2013
    Location
    Longview, TX
    MS-Off Ver
    MS365
    Posts
    338

    Re: Need a macro to sum or subtotal ranges of varying lengths in a column

    I'm off work today, so I won't be able to try this until the middle of next week. I'll be sure to come back & let you know how it goes.

    Oh, Grand Total represents the sum of all the Totals above it (as the report originally has it when it's generated). It's possible for this number to be wrong (sometimes the report doesn't have all of the information on it that it's supposed to), that's why the need for a formula to sum up the Totals.

    Thanks a lot! And I'll report back next week.

    Jenny

  10. #10
    Forum Contributor
    Join Date
    06-25-2013
    Location
    Longview, TX
    MS-Off Ver
    MS365
    Posts
    338

    Re: Need a macro to sum or subtotal ranges of varying lengths in a column

    Hi John,
    I'm back! Had to take care of some month-end work.

    I wanted to confine the macro to just the top section of the worksheet - the Summary section. So, I made a couple of additions to your already awesome code! I want it to stop when it gets to the row with "Grand Total" in column F. So, here's what I have now:
    Please Login or Register  to view this content.
    My only remaining problem is that, near the end of the code, it specifies J13:J31, which is how long the section is on the workbook I posted. But the Summary section is always longer than that, I just never know HOW long it's going to be. I tried replacing "=SUM(J13:J31)" with "=SUM("J13:J" & SL.Row - 3)", but that gives me a Compile error: Expected: end of statement on the J13. So, apparently, I can't use the SL.Row - 3 there or else I've got the syntax wrong. Ideas?

    Thank you for your help!

    Jenny

    Edited: I got the Compile error to go away by using double quotes around J13:J. So, now it recognizes the SL.Row - 3, but it seems to want to back up too many lines. So, on the line that cuts the empty rows, it cuts clear up & includes the last Total row. And the Grand Totals return #NAME, plus the last cells in J and K don't get cleared. I'm not sure what I did wrong, but I'll attach the latest version of the workbook. And, now the end of the code looks like this:
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by zookeepertx; 05-07-2014 at 05:27 PM. Reason: to add new information

  11. #11
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need a macro to sum or subtotal ranges of varying lengths in a column

    Jenny:

    Can you provide a current report with a before and after?

  12. #12
    Forum Contributor
    Join Date
    06-25-2013
    Location
    Longview, TX
    MS-Off Ver
    MS365
    Posts
    338

    Re: Need a macro to sum or subtotal ranges of varying lengths in a column

    Quote Originally Posted by JOHN H. DAVIS View Post
    Jenny:

    Can you provide a current report with a before and after?
    Hi John,

    Sure. The first sheet of the attached book is before and the second sheet is after the macro as it stands now. The reason I need to confine this part of the macro to only work on the Summary section is that there's a lot of other data below that section that is formatted completely differently than the Summary section. I'm going to be working on incorporating that in the macro next.

    Thanks
    Jenny
    Attached Files Attached Files

  13. #13
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need a macro to sum or subtotal ranges of varying lengths in a column

    Jenny:

    I need an after sheet which shows your desired result.

  14. #14
    Forum Contributor
    Join Date
    06-25-2013
    Location
    Longview, TX
    MS-Off Ver
    MS365
    Posts
    338

    Re: Need a macro to sum or subtotal ranges of varying lengths in a column

    Also, I'm curious - in this line of code:
    Please Login or Register  to view this content.
    what does the (3)(7) mean? I'm thinking that might be part of my problem, but I can't figure it out.

    Jenny
    Last edited by zookeepertx; 05-08-2014 at 09:29 AM.

  15. #15
    Forum Contributor
    Join Date
    06-25-2013
    Location
    Longview, TX
    MS-Off Ver
    MS365
    Posts
    338

    Re: Need a macro to sum or subtotal ranges of varying lengths in a column

    Quote Originally Posted by JOHN H. DAVIS View Post
    Jenny:

    I need an after sheet which shows your desired result.
    Oh, DUH! I'm so slow, LOL! Here you go:
    Attached Files Attached Files

  16. #16
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need a macro to sum or subtotal ranges of varying lengths in a column

    Try:

    Please Login or Register  to view this content.

  17. #17
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need a macro to sum or subtotal ranges of varying lengths in a column

    BTW:

    "what does the (3)(7) mean? I'm thinking that might be part of my problem, but I can't figure it out."

    The (3) means (xlUp). The (7) is a row offset for 8 rows after last cell with data.

  18. #18
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need a macro to sum or subtotal ranges of varying lengths in a column

    Better to only deal with the Summary section, I hope.

    Please Login or Register  to view this content.

  19. #19
    Forum Contributor
    Join Date
    06-25-2013
    Location
    Longview, TX
    MS-Off Ver
    MS365
    Posts
    338

    Re: Need a macro to sum or subtotal ranges of varying lengths in a column

    Quote Originally Posted by JOHN H. DAVIS View Post
    Better to only deal with the Summary section, I hope.

    Please Login or Register  to view this content.
    Beautiful!! I only had to make a few tweaks, because, when I added it to my existing code, there were a few conflicts, but it's working great now! It's AWESOME! Thank you SO much for all your help!
    Later on, I plan to deal with the 2,000 or so lines in the detail section under the Summary section, but that's a whole nother can of worms, LOL! That'll probably be later in the month, so I'll start another thread for that.

    You rock!

    Jenny

  20. #20
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need a macro to sum or subtotal ranges of varying lengths in a column

    You're welcome. Glad to help out and thanks for the feedback.

+ 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. [SOLVED] Generic sort macro for varying lengths
    By s0o0y in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-24-2013, 04:59 PM
  2. VBA to Subtotal in Varying Rows and Ranges
    By snelson0713 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-26-2011, 12:46 PM
  3. Replies: 2
    Last Post: 09-20-2010, 05:55 AM
  4. Apply Macro to varying row lengths
    By izza56 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-08-2006, 10:36 AM
  5. Fill down formula using VBA to varying column lengths
    By Lee Jeffery in forum Excel - New Users/Basics
    Replies: 0
    Last Post: 01-28-2005, 08:40 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