+ Reply to Thread
Results 1 to 41 of 41

VBA Code to Total Sections with Varying Number of Rows

Hybrid View

  1. #1
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    VBA Code to Total Sections with Varying Number of Rows

    I would REALLY like some help with creating a code for section totals and then another code that adds a ?total? row. I just started learning about Macros and VBA codes last week, so this is all pretty new and a little overwhelming to me. I apologize in advance for the long description, it?s hard to explain verbally, let alone capture in words.

    I've attached a sample spreadsheet. I've also attached a snapshot, in case you don't want to open the file.

    For the section totals, those formulas need to go in columns G/I/K/N in the highlighted and bold rows. The problem is, there could be 1 section, or up to 5 or 6 sections, each having a different number of rows. For example, I would like the section total formula in cells G3/I3/K3/N3 to total up the rows below (items 20/30/40), but stop at the last part number before the next section. I would like to do the same for all sections and option sections, rental sections don?t have to have totals.
    There will always be at least 1 section and 1 option section in each quote.
    I added ?higher level item? data in column Q. I don?t currently have that information pulling in, but I can if it will make this easier. The ?higher level item? will identify which section parts belong to in a quote. For example, Section A in Quote 1 is Item 10, so every part in Section A will have a higher level item of 10. As you can see Quote 4 also has a Section A that is item 10. Part of the VBA code I currently have will insert 2 rows between quote numbers that are different, so hopefully that helps too.

    For the total row, I would like to insert 2 rows above the first option section in each quote. In the first row that was inserted is where I would like ?total? in column D with right aligned. I would like the total formulas in columns G/I/K/N to add up the section totals.

    For the full scope, I will also need formulas added to the financial summary tab in columns E/F/G/N that will pull from the total row for each quote. Information in the first row (row 4) would pull the total for the first quote in the BOM in tab. Second line would pull the information from the second quote and so on.
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,713

    Re: VBA Code to Total Sections with Varying Number of Rows

    Hi Sawyer84 & welcome to the forum

    Try below code ...
    Sub test()
    
    Dim a, Col$
    
    With Sheets("BOM")
       With .Range("D1", .Range("D" & Rows.Count).End(3))
          a = Filter(.Parent.Evaluate(Replace("transpose(iferror(if(search(""section"",@)=1,row(@)),false))", "@", .Address)), False, 0)
          If UBound(a) = -1 Then Exit Sub
          ReDim Preserve a(0 To UBound(a) + 1)
          a(UBound(a)) = .Rows.Count
       End With
       For x = 0 To UBound(a) - 1
          For i = 1 To 4
             Col = Choose(i, "G", "I", "K", "N")
             .Cells(a(x), Col) = "=sum(" & Col & a(x) + 1 & ":" & Col & a(x + 1) - 1 & ")"
          Next
       Next
    End With
    
    End Sub
    If I was able to help, you can thank me by clicking the * Add Reputation under my user name

  3. #3
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    nankw83 Thank you so much for the quick reply. The formula works ALMOST perfectly! The only issue I see is the sum formula in the last Section before the Option Section. In those formulas, they include the Option Sections and Rental Sections in their totals.
    There also isn't a formula for the Option Sections, but I figured once we get the Section formulas down I'll try to recreate that for the Option Sections.

  4. #4
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,713

    Re: VBA Code to Total Sections with Varying Number of Rows

    I went by the blue colored rows ... So formulas should be in rows where there's 'SECTION*', 'OPTION SECTION' & 'RENTAL SECTION' ?

  5. #5
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    Just 'SECTION*' and 'OPTION SECTION', no totals needed for 'RENTAL SECTION'. The range will need to be C rather than D because the description in column D will change sometimes, but column C will never change.

  6. #6
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,713

    Re: VBA Code to Total Sections with Varying Number of Rows

    This is weird ... I posted an updated code yesterday but it disappeared Anyway, try below code

    Sub test()
    
    Dim a, Col$
    
    With Sheets("BOM")
       With .Range("C1", .Range("C" & Rows.Count).End(3))
          a = Filter(.Parent.Evaluate(Replace("transpose(if(isnumber(search(""rental"",@)),false,iferror(if(search(""section"",@)>0,row(@)),false)))", "@", .Address)), False, 0)
          If UBound(a) = -1 Then Exit Sub
          ReDim Preserve a(0 To UBound(a) + 1)
          a(UBound(a)) = .Rows.Count
       End With
       For x = 0 To UBound(a) - 1
          For i = 1 To 4
             Col = Choose(i, "G", "I", "K", "N")
             .Cells(a(x), Col) = "=sum(" & Col & a(x) + 1 & ":" & Col & a(x + 1) - 1 & ")"
          Next
       Next
    End With
    
    End Sub

  7. #7
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    That is weird because I can still see your previous code. Anyway, I tried the code and it added the sum formulas to the option sections. However, the last option section in each quote goes past the last part in that option section. I've attached a couple snapshots to help explain.
    Attached Images Attached Images

  8. #8
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,713

    Re: VBA Code to Total Sections with Varying Number of Rows

    The code simply looks for the cells containing the word "section" & consider the formula for the cells in between .. Does it provide incorrect numbers in your first picture ? In the second picture, it includes the "Rental Section" but the only flaw I have is the last formula should include row #34 as well

  9. #9
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    No sir, the option section formula should only include the parts in the specific option section. So basically, it would need stop before the next bold section. So in snapshot 1 that formula would need to stop at row #14. The formula in snapshot 2 would need to stop at row #29. Rental sections don?t need to be totaled, but if it will make this VBA code easier, totaling the rental section won?t be an issue.
    I know this hasn?t been easy, but I really appreciate your patience and willingness to help me.

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

    Re: VBA Code to Total Sections with Varying Number of Rows

    @Sawyer84

    Probably I can't help you enough, but more general.


    It would be very usefull is you add the expected result manualy in the file.

    Then forummembers can compair the offered solution with the manualy expected result.
    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.

  11. #11
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    I don?t have access to that file right now since I?m not at home, but see below for what the formula should be for rows that are incorrect. I just don?t want to cause confusion because those formulas will change for every quote that is done, since the number of rows will always be different.

    Row 12, Cell G12: =SUM(G13:G14)
    Row 34, Cell G34: =SUM(G28:G29)

  12. #12
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA Code to Total Sections with Varying Number of Rows

    Quote Originally Posted by Sawyer84 View Post
    Row 34, Cell G34: =SUM(G28:G29)
    Hello. Sawyer84.
    Your clarification didn't help much because you were wrong to mention row 34.

    What oeldere asked you to do is upload a second workbook to the Forum that has three sheets:

    - BOM,
    - Co. Cost, and
    - expected BOM

    In this last sheet you will place the expected formulas in the cells: G24, G27, G30 and G33.

  13. #13
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    Goodness, yes, you?re right. That?s what I get for trying to do this on my phone. Correction below. I?ll try to get that spreadsheet uploaded when I get home later today. Rental Sections on rows 30 and 33 don?t need a formula, but if it makes creating the correct VBA code easier, having formulas for the rental sections won?t be an issue.

    Row 12, Cell G12: =SUM(G13:G14)
    Row 27, Cell G27: =SUM(G28:G29)

  14. #14
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    Okay, I think I figured out a way to make the VBA code that nankw83 created. The only formula that is slightly wrong is the very last section on the very last quote. So that will be easy enough to fix or just ignore since the last section is typically a rental section, which don't need a formula anyway. I will mark this as solved. I tried to post the code, but it wouldn't let me, so I've attached a text file and a snapshot.

    nankw83 I am incredibly thankful for all of your help with this.
    Attached Images Attached Images
    Attached Files Attached Files

  15. #15
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA Code to Total Sections with Varying Number of Rows

    Mmm...
    I see that you assume that the value of cell G7 should be 3.

    I ask you: shouldn't it be 10 that represents the sum of all Section B (G8 to G14)?

  16. #16
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    Negative, Section B only consists of the 2 rows below it, rows 8 and 9. Basically, every row that is in bold is it's own section. I've attached a snapshot of what needs to happen next, which I was going to post in a new thread. I just wanted to mention it to you so you have the full picture and may help explain things. Only rows that have Section A, Section B, Section C, etc., in column C, are included in the total. Anything that has Option Section or Rental Section in column C, aren't included in the total. Like I said, my next thread will be about creating a VBA code that will insert 2 rows above the first option section in every quote and that puts the Total text in column D and formulas in columns G/I/K/N that adds the section totals above it. I already have the conditional formatting that will bold and color a row with the text Total in column D
    Attached Images Attached Images

  17. #17
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA Code to Total Sections with Varying Number of Rows

    And why didn't you put it all together?... What a waste of time!

    For your new query, publish only three sheets:

    - BOM
    - Co.Cost
    - expected BOM

    - The first sheet is the one you originally have. I have a suspicion that in your original data you don't have all those empty rows that you showed in your first post.

    - And on the third page you show us what you hope to achieve.
    Last edited by beyond Excel; 10-09-2022 at 05:13 PM.

  18. #18
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    That wasn?t my goal and I apologize if I wasted anyone?s time. I mentioned in my very first post (2nd to last paragraph) that my next goal would be inserting a row that calculates the total. I know my first post was long, so I can understand you not making it all the way to the end to see that information.

  19. #19
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    I've attached the spreadsheet with the sheets like you asked. The original data doesn't have the rows inserted between the quotes, I created a VBA code to do that. However, I found it made the formula work easier if I added the rows between quotes after the sections were totaled.
    Attached Files Attached Files

  20. #20
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA Code to Total Sections with Varying Number of Rows

    As this thread is already finished and -probably- nothing that we have helped you will serve the purposes of the new requirement you have (the most important thing is that your original data does not come with empty rows as I imagined), then you better start a new thread and upload this last workbook.

    Tip: Don't rush to end the thread or give reputation early!

  21. #21
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA Code to Total Sections with Varying Number of Rows

    Sawyer84,

    try
    Sub test()
        Dim x, i As Long, s
        Application.ScreenUpdating = False
        With Sheets("bom").Cells(1).CurrentRegion
            x = Filter(.Parent.[transpose(if((left(c3:c10000,7)="OPTION ")+(right(c3:c10000,1)="A")+(row(3:10000)=3),row(3:10000)))], False, 0)
            For i = UBound(x) To 1 Step -1
                s = .Cells(x(i), 3)
                .Rows(x(i)).Resize(2).Insert
                If s Like "OPTION *" Then
                    With .Rows(x(i))
                        .Font.Bold = True: .Interior.Color = rgbDarkOrange
                        .Range("d1") = "TOTAL": .Range("d1").HorizontalAlignment = xlRight
                        .Range("g1,i1,k1,n1:p1").FormulaR1C1 = "=sum(r" & x(i - 1) & "c:r[-1]c)"
                    End With
                End If
            Next
        End With
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files

  22. #22
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    jindon, thank you for that. It is close to working perfectly. The only thing it didn't do correctly was the total formula. Instead of adding only the section totals (rows 3 & 7), it added everything. I've attached a snapshot of what it did (1st snapshot) and then what it should be doing (2nd snapshot).
    Attached Images Attached Images

  23. #23
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA Code to Total Sections with Varying Number of Rows

    Didn't notice the formula in top of each SECTION.
    try
    Sub test()
        Dim x, i As Long, s
        Application.ScreenUpdating = False
        With Sheets("bom").Cells(1).CurrentRegion
            x = Filter(.Parent.[transpose(if((isnumber(search("SECTION",c3:c10000)))+(row(3:10000)=3)+(row(3:10000)=max(if(c3:c10000<>"",row(3:10000)))),row(3:10000)))], False, 0)
            For i = 0 To UBound(x) - 1
                If UCase$(.Cells(x(i), 3)) Like "SECTION *" Then
                    .Rows(x(i)).Range("g1,i1,k1,n1:p1").FormulaR1C1 = "=subtotal(9,r" & x(i) + 1 & "c:r" & x(i + 1) - 1 & "c)"
                End If
            Next
            x = Filter(.Parent.[transpose(if((left(c3:c10000,7)="OPTION ")+(right(c3:c10000,1)="A")+(row(3:10000)=3),row(3:10000)))], False, 0)
            For i = UBound(x) To 1 Step -1
                s = .Cells(x(i), 3)
                .Rows(x(i)).Resize(2).Insert
                If s Like "OPTION *" Then
                    With .Rows(x(i))
                        .Font.Bold = True: .Interior.Color = rgbDarkOrange
                        .Range("d1") = "TOTAL": .Range("d1").HorizontalAlignment = xlRight
                        .Range("g1,i1,k1,n1:p1").FormulaR1C1 = "=subtotal(9,r" & x(i - 1) & "c:r[-1]c)"
                    End With
                End If
            Next
        End With
        Application.ScreenUpdating = True
    End Sub

  24. #24
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    Holy smokes, that worked perfectly!! Thank you so much!

  25. #25
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA Code to Total Sections with Varying Number of Rows

    You are welcome and thanks for the rep,

    When you need to Sum only the summed row(s) within the specific range, Subtotal function should be the way to calculate.

  26. #26
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    That actually makes a lot of sense after seeing how you did that. I appreciate the tip.

  27. #27
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    jindon, after a second glance I had to modify your code slightly since it was putting the subtotal in columns O and P. It works, but columns O and P in the new Total row are blank. Is there anyway to modify this code to copy the formula either above or below? See below for the pre-loaded formula in those columns.

    Column O formula: =(N10-G10)/N10
    Column P formula: =(N10-I10)/N10

    Sub test()
    
        Dim x, i As Long, s
        Application.ScreenUpdating = False
        With Sheets("bom").Cells(1).CurrentRegion
            x = Filter(.Parent.[transpose(if((isnumber(search("SECTION",c3:c10000)))+(row(3:10000)=3)+(row(3:10000)=max(if(c3:c10000<>"",row(3:10000)))),row(3:10000)))], False, 0)
            For i = 0 To UBound(x) - 1
                If UCase$(.Cells(x(i), 3)) Like "SECTION *" Then
                    .Rows(x(i)).Range("g1,i1,k1,n1").FormulaR1C1 = "=subtotal(9,r" & x(i) + 1 & "c:r" & x(i + 1) - 1 & "c)"
                End If
            Next
            x = Filter(.Parent.[transpose(if((left(c3:c10000,7)="OPTION ")+(right(c3:c10000,1)="A")+(row(3:10000)=3),row(3:10000)))], False, 0)
            For i = UBound(x) To 1 Step -1
                s = .Cells(x(i), 3)
                .Rows(x(i)).Resize(2).Insert
                If s Like "OPTION *" Then
                    With .Rows(x(i))
                        .Font.Bold = True: .Interior.Color = rgbDarkOrange
                        .Range("d1") = "TOTAL": .Range("d1").HorizontalAlignment = xlRight
                        .Range("g1,i1,k1,n1").FormulaR1C1 = "=subtotal(9,r" & x(i - 1) & "c:r[-1]c)"
                    End With
                End If
            Next
        End With
        Application.ScreenUpdating = True
        
    End Sub

  28. #28
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    @jindon, sorry for the back to back messages, but I just tried this your original code on an existing spreadsheet with multiple quotes and it didn't insert the Total rows correctly, see snapshot below. Often times there are multiple options sections in one quote and I think that is what's causing this issue.
    Snapshot 1 (Quote 1): Rows 15 and 16 shouldn't be there
    Snapshot 2 (Quote 4): Rows 42 and 43 shouldn't be there
    Attached Images Attached Images

  29. #29
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA Code to Total Sections with Varying Number of Rows

    See if this works as you want.
    Sub test()
        Dim x, i As Long, s(1)
        Application.ScreenUpdating = False
        With Sheets("bom").Cells(1).CurrentRegion
            x = Filter(.Parent.[transpose(if((isnumber(search("SECTION",c3:c10000)))+(row(3:10000)=3)+(row(3:10000)=max(if(c3:c10000<>"",row(3:10000)))),row(3:10000)))], False, 0)
            For i = 0 To UBound(x) - 1
                If UCase$(.Cells(x(i), 3)) Like "SECTION *" Then
                    .Rows(x(i)).Range("o1:p1").FormulaR1C1 = Array("=(rc[-1]-rc7)/rc[-1]", "=(rc[-2]-rc9)/rc[-2]")
                    .Rows(x(i)).Range("g1,i1,k1,n1").FormulaR1C1 = "=subtotal(9,r" & x(i) + 1 & "c:r" & x(i + 1) - 1 & "c)"
                End If
            Next
            x = Filter(.Parent.[transpose(if((left(c3:c10000,7)="OPTION ")+(right(c3:c10000,1)="A")+(row(3:10000)=3),row(3:10000)))], False, 0)
            For i = UBound(x) To 1 Step -1
                s(0) = .Cells(x(i), 3): s(1) = .Cells(x(i - 1), 3)
                If (s(0) Like "* A") + (s(1) Like "* A") Then
                    .Rows(x(i)).Resize(2).Insert
                    If s(0) Like "OPTION *" Then
                        With .Rows(x(i))
                            .Font.Bold = True: .Interior.Color = "&H8CFF"
                            .Range("d1") = "TOTAL": .Range("d1").HorizontalAlignment = xlRight
                            .Range("o1:p1").FormulaR1C1 = Array("=(rc[-1]-rc7)/rc[-1]", "=(rc[-2]-rc9)/rc[-2]")
                            .Range("g1,i1,k1,n1").FormulaR1C1 = "=subtotal(9,r" & x(i - 1) & "c:r[-1]c)"
                        End With
                    End If
                End If
            Next
        End With
        Application.ScreenUpdating = True
    End Sub

  30. #30
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    That looks like a winner. I'll continue to keep testing it. Thanks again for all of your help.

  31. #31
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    @jindon, I did find one issue. Sometimes there is only 1 section on a quote, it's typically Section A, but sometimes it might be a different Section. In this scenario, I had a spreadsheet that had a quote with only Section D and it wouldn't put spaces between the quote above it, or add the total row. See snapshot below. There should be 2 rows above row 18 to separate quote 1 and quote 2 and then there should be 2 rows above row 21, which include the total.
    Attached Images Attached Images

  32. #32
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA Code to Total Sections with Varying Number of Rows

    I will have a look at it sometime tomorrow, hopefully.

  33. #33
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    Thank you very much

  34. #34
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA Code to Total Sections with Varying Number of Rows

    Try this one
    Sub test()
        Dim a, x, i As Long, s(1)
        Application.ScreenUpdating = False
        With Sheets("bom").Cells(1).CurrentRegion
            a = .Value: s(1) = .Parent.Evaluate("max(if(" & .Columns(3).Address & "<>"""",row(" & .Address & ")))")
            With CreateObject("Scripting.Dictionary")
                For i = 3 To UBound(a, 1)
                    If (a(i, 3) Like "SECTION *") + (a(i, 3) Like "OPTION *") Then
                        s(0) = Join(Array(a(i, 1), Split(a(i, 3))(0)), Chr(2))
                        If Not .exists(s(0)) Then .Item(s(0)) = i
                    End If
                Next
                .Item(i) = s(1)
                x = .items
            End With
            For i = 0 To UBound(x) - 1
                If UCase$(.Cells(x(i), 3)) Like "SECTION *" Then
                    .Rows(x(i)).Range("o1:p1").FormulaR1C1 = Array("=(rc[-1]-rc7)/rc[-1]", "=(rc[-2]-rc9)/rc[-2]")
                    .Rows(x(i)).Range("g1,i1,k1,n1").FormulaR1C1 = "=subtotal(9,r" & x(i) + 1 & "c:r" & x(i + 1) - 1 & "c)"
                End If
            Next
            For i = UBound(x) To 1 Step -1
                s(0) = .Cells(x(i), 3): s(1) = .Cells(x(i - 1), 3)
                If (s(0) Like "SECTION*") + (s(1) Like "SECTION*") Then
                    .Rows(x(i)).Resize(2).Insert
                    If s(0) Like "OPTION *" Then
                        With .Rows(x(i))
                            .Font.Bold = True: .Interior.Color = "&H8CFF"
                            .Range("d1") = "TOTAL": .Range("d1").HorizontalAlignment = xlRight
                            .Range("o1:p1").FormulaR1C1 = Array("=(rc[-1]-rc7)/rc[-1]", "=(rc[-2]-rc9)/rc[-2]")
                            .Range("g1,i1,k1,n1").FormulaR1C1 = "=subtotal(9,r" & x(i - 1) & "c:r[-1]c)"
                        End With
                    End If
                End If
            Next
        End With
        Application.ScreenUpdating = True
    End Sub
    Last edited by jindon; 10-12-2022 at 11:17 AM.

  35. #35
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    Okay, so I'm kind of lost now and I will COMPLETELY understand if you want to give up on this. I know you've already spent a lot of your time on this. However, if you are still willing to help, that would be incredible. So the issue with your code is the first section in each quote includes the total of the entire quote, so it acts just like the total row. The other issue is that there isn't a formula for each section and option section. So, I modified your other code below and the only issue with this code is that it only works correctly if the first section in a quote is Section A. If it could be modified to work if the first section in a quote is Section B, Section C, Section D, Section E, Section F, or Section G, then that would be great.

    Sub test()
        Dim x, i As Long, s(1)
        Application.ScreenUpdating = False
        With Sheets("bom").Cells(1).CurrentRegion
            x = Filter(.Parent.[transpose(if((isnumber(search("SECTION",c3:c10000)))+(row(3:10000)=3)+(row(3:10000)=max(if(c3:c10000<>"",row(3:10000)))),row(3:10000)))], False, 0)
            For i = 0 To UBound(x) - 1
                If UCase$(.Cells(x(i), 3)) Like "SECTION *" Then
                    .Rows(x(i)).Range("o1:p1").FormulaR1C1 = Array("=(rc[-1]-rc7)/rc[-1]", "=(rc[-2]-rc9)/rc[-2]")
                    .Rows(x(i)).Range("g1,i1,k1,n1").FormulaR1C1 = "=subtotal(9,r" & x(i) + 1 & "c:r" & x(i + 1) - 1 & "c)"
                End If
            Next
            For i = 0 To UBound(x) - 1
                If UCase$(.Cells(x(i), 3)) Like "OPTION *" Then
                    .Rows(x(i)).Range("o1:p1").FormulaR1C1 = Array("=(rc[-1]-rc7)/rc[-1]", "=(rc[-2]-rc9)/rc[-2]")
                    .Rows(x(i)).Range("g1,i1,k1,n1").FormulaR1C1 = "=subtotal(9,r" & x(i) + 1 & "c:r" & x(i + 1) - 1 & "c)"
                End If
            Next
            x = Filter(.Parent.[transpose(if((left(c3:c10000,7)="OPTION ")+(right(c3:c10000,1)="A")+(row(3:10000)=3),row(3:10000)))], False, 0)
            For i = UBound(x) To 1 Step -1
                s(0) = .Cells(x(i), 3): s(1) = .Cells(x(i - 1), 3)
                If (s(0) Like "* A") + (s(1) Like "* A") Then
                    .Rows(x(i)).Resize(2).Insert
                    If s(0) Like "OPTION *" Then
                        With .Rows(x(i))
                            .Font.Bold = True:
                            .Range("d1") = "TOTAL": .Range("d1").HorizontalAlignment = xlRight
                            .Range("o1:p1").FormulaR1C1 = Array("=(rc[-1]-rc7)/rc[-1]", "=(rc[-2]-rc9)/rc[-2]")
                            .Range("g1,i1,k1,n1").FormulaR1C1 = "=subtotal(9,r" & x(i - 1) & "c:r[-1]c)"
                        End With
                    End If
                End If
            Next
        End With
        Application.ScreenUpdating = True
    End Sub

  36. #36
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA Code to Total Sections with Varying Number of Rows

    Working with picture is too hard.
    If you upload a workbook with all the possible situations that will occur and the result that you want, I will look at it tomorrow.
    If you don't want to do it, just ignore this post.

  37. #37
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    I will absolutely do that. I will upload it tomorrow morning my time, which will probably towards the end of your day.

  38. #38
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    Okay, I've uploaded the spreadsheet. The 'Expected BOM' is what I would like it to look like.
    Attached Files Attached Files

  39. #39
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA Code to Total Sections with Varying Number of Rows

    Let's see if I missed anything.
    This code also accepts with or without any OPTION block(s) within a QUOTE.
    Sub test()
        Dim a, x, y, t, i As Long, flg As Boolean
        Application.ScreenUpdating = False
        With Sheets("bom").Cells(1).CurrentRegion
            x = Filter(.Parent.[transpose(if((isnumber(search("SECTION",c3:c10000)))+(row(3:10000)=3)+(row(3:10000)=max(if(c3:c10000<>"",row(3:10000)))),row(3:10000)))], False, 0)
            a = .Resize(x(UBound(x))).Value
            For i = 0 To UBound(x) - 1
                If UCase$(.Cells(x(i), 3)) Like "SECTION *" Then
                    .Rows(x(i)).Range("o1:p1").FormulaR1C1 = Array("=(rc[-1]-rc7)/rc[-1]", "=(rc[-2]-rc9)/rc[-2]")
                    .Rows(x(i)).Range("g1,i1,k1,n1").FormulaR1C1 = "=subtotal(9,r" & x(i) + 1 & "c:r" & x(i + 1) - 1 & "c)"
                Else
                    If .Cells(x(i), 3) Like "OPTION *" Then
                        y = .Parent.Evaluate("min(if((isnumber(search(""SECTION"",c3:c10000)))*(row(3:10000)>" & x(i) & "),row(3:10000)))")
                        If y = 0 Then y = UBound(a, 1)
                        .Rows(x(i)).Range("o1:p1").FormulaR1C1 = Array("=(rc[-1]-rc7)/rc[-1]", "=(rc[-2]-rc9)/rc[-2]")
                        .Rows(x(i)).Range("g1,i1,k1,n1").FormulaR1C1 = "=subtotal(9,r" & x(i) + 1 & "c:r" & y - 1 & "c)"
                    End If
                End If
            Next
            With CreateObject("Scripting.Dictionary")
                For i = UBound(a, 1) To 3 Step -1
                    If a(i, 1) <> "" Then
                        If Not .exists(a(i, 1)) Then .Item(a(i, 1)) = Array(0, i, i)
                        If a(i, 3) Like "OPTION *" Then
                            .Item(a(i, 1)) = Array(.Item(a(i, 1))(0), i, .Item(a(i, 1))(2))
                        ElseIf a(i, 3) Like "SECTION *" Then
                            .Item(a(i, 1)) = Array(i, .Item(a(i, 1))(1), .Item(a(i, 1))(2))
                        End If
                    End If
                Next
                x = .items
            End With
            For i = 0 To UBound(x)
                If i > 0 Then .Rows(x(i)(2) + 1).Resize(2).Insert: t = x(i)(2) + 1
                If x(i)(1) <> x(i)(2) Then
                    .Rows(x(i)(1)).Resize(2).Insert: t = x(i)(1)
                Else
                    t = x(i)(2) + 1
                End If
                With .Rows(t)
                    .Font.Bold = True: .Interior.Color = "&H8CFF"
                    .Range("d1") = "TOTAL": .Range("d1").HorizontalAlignment = xlRight
                    .Range("o1:p1").FormulaR1C1 = Array("=(rc[-1]-rc7)/rc[-1]", "=(rc[-2]-rc9)/rc[-2]")
                    .Range("g1,i1,k1,n1").FormulaR1C1 = "=subtotal(9,r" & x(i)(0) & "c:r[-1]c)"
                End With
            Next
        End With
        Application.ScreenUpdating = True
    End Sub

  40. #40
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    Awesome, I'm checking it out now.

  41. #41
    Registered User
    Join Date
    10-05-2022
    Location
    Houston
    MS-Off Ver
    365
    Posts
    24

    Re: VBA Code to Total Sections with Varying Number of Rows

    Well it's official, you're a genius! lol. For real, I've ran some tests on real life quotes and I haven't found an issue. I literally can't thank you enough for all of your hard work, I don't believe a lot of people would go above and beyond like you did. Thanks again.

+ 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. How to total varying numbers of rows
    By Involute in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-06-2017, 11:05 AM
  2. [SOLVED] Total units made by week number with varying manufacturing periods
    By BRISBANEBOB in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-09-2016, 06:49 PM
  3. [SOLVED] VBA code to copy and paste a varying number of Rows
    By sx200n in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-10-2015, 08:57 AM
  4. Sum total for varying number of cells
    By peri1224 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 11-30-2009, 12:34 PM
  5. Averaging A Varying Total Across A Number Of Cells
    By xStephaniex in forum Excel General
    Replies: 1
    Last Post: 06-08-2008, 10:47 AM
  6. How To Deal With Varying Number of Rows
    By messnj in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-18-2007, 02:40 AM
  7. Sum varying number of rows
    By LSkarbek in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-12-2006, 11:10 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