+ Reply to Thread
Results 1 to 12 of 12

Macro for multiple subtotals

Hybrid View

  1. #1
    Registered User
    Join Date
    03-26-2015
    Location
    Florida
    MS-Off Ver
    Office 2007
    Posts
    25

    Macro for multiple subtotals

    Hello, i'm in need of some VBA help for an excel macro. I have a macro that takes data from one sheet and moves the relevant/needed data to a second sheet within the same workbook. Once the data is moved to a new sheet, the macro then separates the data based on the date by putting two lines in between each set of data as shown in the attached picture. What I would like to be able to do is subtotal each little group (generally anywhere between 1 and 3 lines) under the hours which is column J and that's where i'm stuck. I have everything else working properly except I am stumped on how to get column J subtotals for each little group to work. Any help would be greatly appreciated!

    sample.JPG
    Attached Images Attached Images
    Last edited by orlex; 10-26-2015 at 04:45 PM.

  2. #2
    Registered User
    Join Date
    08-01-2012
    Location
    Montreal, Qc, Canada
    MS-Off Ver
    Excel 2010
    Posts
    82

    Re: Macro for multiple subtotals

    Hi


    this is how I would have done it... (there may be better ways, im no expert) ... but hope it helps


    
    Sub MySubTotals()
    
    Dim i, Lrow, MySubTotal
    
    i = 2 'use this to loop through rows
    
    
    'find the last row in column "J"
    Lrow = Sheet1.Range("J99999").End(xlUp).Row + 1
    
    
    ' begin loop
    
    Do While i <= Lrow
            
            MySubTotal = 0
                
                'inner loop while hours are present
                
                Do While Sheet1.Cells(i, 10) <> ""
                    
                    MySubTotal = MySubTotal + Sheet1.Cells(i, 10)
                
                i = i + 1
                Loop
                'add the sub total below last row of hours
                Sheet1.Cells(i, 10) = MySubTotal
                
            i = i + 1
            
    Loop
    
    
    End Sub
    If I helped, Don't forget to add to my reputation

  3. #3
    Registered User
    Join Date
    03-26-2015
    Location
    Florida
    MS-Off Ver
    Office 2007
    Posts
    25

    Re: Macro for multiple subtotals

    Unfortunately that didn't work for my case. It actually ended up doing nothing. Any other ideas?

  4. #4
    Registered User
    Join Date
    08-01-2012
    Location
    Montreal, Qc, Canada
    MS-Off Ver
    Excel 2010
    Posts
    82

    Re: Macro for multiple subtotals

    any chance you can post an example of the data?

  5. #5
    Registered User
    Join Date
    03-26-2015
    Location
    Florida
    MS-Off Ver
    Office 2007
    Posts
    25

    Re: Macro for multiple subtotals

    Attached are two samples of the sheet that comes out once I get it separated into a second sheet. I need each small group that pertains to whatever date, to have it subtotal below the last row of that group.

    Sample one: This sample has it before I run part of my macro that separates the rows into groups based on the date.

    Sample two: This sample is what happens after the macro has been run to separate the rows into groups based on the date.
    Attached Files Attached Files
    Last edited by orlex; 10-26-2015 at 03:37 PM.

  6. #6
    Registered User
    Join Date
    08-01-2012
    Location
    Montreal, Qc, Canada
    MS-Off Ver
    Excel 2010
    Posts
    82

    Re: Macro for multiple subtotals

    in your examples (both) your data is on sheet2. This is why my code did not work.

    I have adjust it it accordingly.

    I have also provide you with one to remove the sub totals we create with the first one... just in case

    I only created this for the "SAMPLE DATA post macro"

    I've tested it, and it workd


    
    Sub MySubTotals()
    
    Dim i, lrow, MySubTotal
    
    i = 2 'use this to loop through rows
    
    
    'find the last row in column "J"
    lrow = Sheet2.Range("J99999").End(xlUp).Row + 1
    
    
    ' begin loop
    
    Do While i <= lrow
            
            MySubTotal = 0
                
                'inner loop while hours are present
                
                Do While Sheet2.Cells(i, 10) <> ""
                    
                    MySubTotal = MySubTotal + Sheet2.Cells(i, 10)
                
                i = i + 1
                Loop
                
                'add the sub total below last row of hours
                If MySubTotal <> 0 Then
                    Sheet2.Cells(i, 9) = "SubTotal"
                    Sheet2.Cells(i, 10) = MySubTotal
                End If
                
                
            i = i + 1
            
    Loop
    
    
    End Sub
    
    
    Sub remove_subtotal()
    
    Dim i, lrow
        
    i = 2 'use this to loop through rows
    
    
    'find the last row in column "J"
    lrow = Sheet2.Range("J99999").End(xlUp).Row + 1
        
        
        Do While i <= lrow
        
            If Sheet2.Cells(i, 9) = "SubTotal" Then
                Sheet2.Cells(i, 9) = ""
                Sheet2.Cells(i, 10) = ""
            End If
            i = i + 1
        Loop
        
        
    End Sub

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

    Re: Macro for multiple subtotals

    Try the attached
    Sub test()
        Dim r As Range, i As Long
        Application.ScreenUpdating = False
        Columns("e").Insert
        With Range("d3", Range("d" & Rows.Count).End(xlUp)).Offset(, 1)
            .Formula = "=if(d2<>d3,if(e1=2,""a"",1),"""")"
            .Value = .Value
            On Error Resume Next
            For i = 1 To 2
                .SpecialCells(2, 1).EntireRow.Insert
                .SpecialCells(2, 2).EntireRow.Insert
            Next
            On Error GoTo 0
        End With
        Columns("e").Delete
        With Columns("j").SpecialCells(2, 1)
            For Each r In .Areas
                r(r.Count + 1).Formula = "=subtotal(9," & r.Address & ")"
            Next
        End With
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    03-26-2015
    Location
    Florida
    MS-Off Ver
    Office 2007
    Posts
    25

    Re: Macro for multiple subtotals

    Thank you Jindon, that worked perfectly.

    I appreciate your effort Steve7913
    Last edited by orlex; 10-26-2015 at 04:08 PM.

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

    Re: Macro for multiple subtotals

    You are welcome.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  10. #10
    Registered User
    Join Date
    03-26-2015
    Location
    Florida
    MS-Off Ver
    Office 2007
    Posts
    25

    Re: Macro for multiple subtotals

    Quote Originally Posted by jindon View Post
    You are welcome.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.
    Jindon, I actually I have one final question. Is there a way to make the subtotals bold and if the subtotal is under 8.0, make the subtotal text red?

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

    Re: Macro for multiple subtotals

    Try change
                r(r.Count + 1).Formula = "=subtotal(9," & r.Address & ")"
    to
                With r(r.Count + 1)
                    .Formula = "=subtotal(9," & r.Address & ")"
                    .Font.Bold = True
                    If .Value < 8 Then .Font.Color = vbRed
                End With

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

    Re: Macro for multiple subtotals

    OOps, typo
            .Formula = "=if(d2<>d3,if(e1=2,""a"",1),"""")"
    should be
            .Formula = "=if(d2<>d3,if(e2=1,""a"",1),"""")"

+ 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. Add Multiple Subtotals
    By pociners in forum Excel Charting & Pivots
    Replies: 6
    Last Post: 11-25-2014, 05:12 AM
  2. [SOLVED] subtotals with multiple layers
    By plamb in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-25-2013, 02:20 PM
  3. Macro to apply subtotals to multiple sheets
    By Scotbot in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-20-2012, 09:10 AM
  4. [SOLVED] Subtotals multiple columns
    By john liem in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 PM
  5. [SOLVED] Subtotals multiple columns
    By Gary L Brown in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-06-2005, 04:05 AM
  6. Subtotals multiple columns
    By john liem in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 03:05 AM
  7. Subtotals multiple columns
    By john liem 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