+ Reply to Thread
Results 1 to 6 of 6

Macro to Group Rows and Columns With Many Layers And Add Subtotaling

  1. #1
    Registered User
    Join Date
    08-10-2011
    Location
    Cali
    MS-Off Ver
    Excel 365
    Posts
    62

    Macro to Group Rows and Columns With Many Layers And Add Subtotaling

    Working with a large set of budget data (same attached) and trying to automate the grouping and entering of formulas (this is exported raw out of another database and so I have been manually entering formulas and grouping).

    Nothing comes in colored the way it is, just raw data, but this is a snip from one of the files I did manually. Beige rows have numbers except in the total column which is a formula. Grey, Blue, and Orange rows ALL have formulas for every cell.

    Basically, the formulas I am looking for are (simply put) as follows:


    Brown lines in Col 1, Col 2, Col 3 sum UP into the orange subtotal/group line above. In this example, B5=SUM(B6:B7) B9=SUM(B10:B20), etc etc. This is true except for the total column (column E) which sums ACROSS, which in this example is E9=SUM(B9:D9), E14=SUM(B14:D14), etc, etc. The summing across for totals is true for every row regardless of color.

    Orange lines sum UP into the corresponding blue line above it

    Blue lines sum UP into the corresponding grey line above it

    This dataset has something like 6 grey lines, 50 blue lines, and 100+ orange lines, so lots and lots of formulas.

    I also, as shown in the sample, have them grouped so they will collapse by level (greys expand to show the blues below it, blues expand to show the orange below it, orange expands to show beige below).

    Overall I think the structure is pretty simple, but I am trying to find an efficient starting point for how to count down the rows and figure out what formulas, coloring, and grouping should be applied. In effect I need to start at the top and look what would be the first grey line, then count to first blue, next blue, next blue, etc and then group so the collapse/expand button is on the top grey line. Do that for what would be all the grey lines, then move on to the first blue and grab all the oranges below it, and then to the oranges and grab all the beige below each of those.

    The only reference data I have to differentiate what a line is is the text/numbers in column A. I can verbally explain what I need to do and understand just looking at raw un-formatted data what rolls up to what, but am having issues translating it to code that will do it for me.

    Any help appreciate.

    THanks,
    Attached Files Attached Files

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to Group Rows and Columns With Many Layers And Add Subtotaling

    Hello Norcal1,

    Column "A" looks to me like a company's chart of account numbers for Liabilities and Expenses. My first question about the data is do you need the formulas when the data is exported?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    08-10-2011
    Location
    Cali
    MS-Off Ver
    Excel 365
    Posts
    62

    Re: Macro to Group Rows and Columns With Many Layers And Add Subtotaling

    Very similar, its some tracking codes for various activities (this example is kind of a made up scenario that I was testing with, but the end format is the same).

    We are performing some manipulation/analysis on the raw numbers outside of the tracking database, so I don't need the original formulas to pull through with it. This is more for manually adding projections later on.

    I have gotten far enough using For loops and a helper column added in Column A (pushing everything right to start at column B) to assign a hierarchy number to each row (1 being largest subtotal group which then rolls up all the 2 subtotals below that which roll up all the 3 subtotals before that and finally the level 4 raw data rows below the 3).

    This has allowed me to properly color the lines, but now I need to figure out how to count (in a big nested loop i imagine) from the first row which has a 1 in column, offset down one row, and then select everything until I hit the next 1 in column A and then group (the group part is easy, its the code to figure out what my selection between group levels that I'm stuck on).

    EDIT:

    Trying to use a for, if, and do until loop to basically run down the rows until it finds a "1" in column A. Then I want it to look at the next row and see if column A has a "1" and if not, select the current column A cell and move to the next row. Do this until it finds a "1" in column A again (the Do Until) and then take the row address of the last selected cell that didn't have a "1" in column A. Then select all rows from the first row that it found "1" in column A all the way to one row above the next row that it found with "1" in column A.

    Please Login or Register  to view this content.
    Code looks like that (was just trying to get it to select for now and then group if it worked) but it seems to loop forever and freeze up.

    I should note I have taught myself little to nothing about error avoidance and infinite loops and such yet, but this seems to be one of those instances.

    Thanks,
    Last edited by Norcal1; 10-25-2018 at 08:08 PM.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to Group Rows and Columns With Many Layers And Add Subtotaling

    Hello Norcal1,

    Since you have color coded the cells manually, is there any reliable way to identify which cell belongs to which group? Manually coloring or adding hierarchy numbers takes too much time. Automation depends on recognizable patterns.

  5. #5
    Registered User
    Join Date
    08-10-2011
    Location
    Cali
    MS-Off Ver
    Excel 365
    Posts
    62
    So I have managed to get the coloring right. I'm posting from my phone now so I can add the code in a bit when I get to a computer, but I added a new column A as a helper column. There was enough distinction between the rows in the "Phase" column (now Col B) that I was able to tag the rows as a 1, 2, 3, or 4. 4 being just data, 4s roll up into a 3 above, 3s roll up into a 2 above, 2s roll up into a one above. Did that with For loops and an if statement, one cycle for each hierarchy level. Then another For/If to color the rows appropriately.

    I've gotten that far, just trying to figure out how to count from one subtotal level, say a 3 in row 50, to the next 3 which might be in row 63. I would then select rows 51-62 and group them.

    Quote Originally Posted by Leith Ross View Post
    Hello Norcal1,

    Since you have color coded the cells manually, is there any reliable way to identify which cell belongs to which group? Manually coloring or adding hierarchy numbers takes too much time. Automation depends on recognizable patterns.

  6. #6
    Registered User
    Join Date
    08-10-2011
    Location
    Cali
    MS-Off Ver
    Excel 365
    Posts
    62

    Re: Macro to Group Rows and Columns With Many Layers And Add Subtotaling

    Here is the code I used to insert a new column A and then use it as a helper column to label what "level" each row belongs to (1, 2, 3 or 4):

    Please Login or Register  to view this content.
    This gives me a level assignment in Column A, and then I used the letter in column A to assign colors to the rows.

    Next step is to figure out how to count through the rows and grab the right rows to group. In the sample I attached again, what (I think) I should do is look down Column A row by Row. First it will find a "1" in Row 3, so it should then look down Column A until it finds the next "1" in row 77 and then select all rows BETWEEN those two rows (Select Rows 4 through 76) and group them. Do that for all the "1's" in Column A til the end of the data.

    Next for Level "2", it should look down Column A again and in row 4 find a "2", then find the next "2" in row 8. It should select rows 5 through 7 and group them, then it would next find a "2" in row 8 and go down to find the next in row 42 and select rows 9 through 41 and group them, and on again.

    Level 3 is slightly different because when you reach a "3" in Column A, the next "break" if you will could be a "1" OR a "2" OR a "3", but whatever the first one to come is that will always be the "end" of that group and beginning of the next.

    If I were to ignore all the data and just look at the numbers in Column A, I've got to figure how to look down the column, find a number and store that row and then find the very next instance of that number and store that row, then just use the Rows().Select to grab that group of rows. From there I think I can figure out how to group and add formulas by rule.

    The loop(s) i posted above seem to through my into and infinite loop but so far I have not been able to diagnose why. First time using a Do until so that's probably the deal.
    Attached Files Attached Files

+ 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. Deleting rows totaling zero before subtotaling macro runs
    By CStumpo in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 06-01-2018, 12:07 PM
  2. macro to group rows and columns.
    By lalaarif1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-24-2016, 11:48 AM
  3. macro to group rows and columns.
    By lalaarif1 in forum Excel General
    Replies: 0
    Last Post: 12-24-2016, 11:48 AM
  4. Macro to Group Rows with Zeros in Multiple Columns
    By matt-s in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 04-25-2013, 05:51 PM
  5. Replies: 0
    Last Post: 03-28-2013, 02:35 PM
  6. [SOLVED] Subtotaling based on more than one group
    By Anthony in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 PM
  7. [SOLVED] Subtotaling based on more than one group
    By Anthony in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 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