Macro Loop Help!! - Code to Insert Totals into Each Sheet within Workbook - Excel 2013

magnum5az

New Member
Joined
Jun 21, 2017
Messages
16
Hello all, I am working on a piece of code that currently works fine if you trigger the code sheet by sheet manually, but when I add a loop code it keeps entering the subtotals in the same sheet over and over again? Any help is appreciated!

Code:
Sub forEachWs2()

    
    Dim ws As Worksheet
    
    
    For Each ws In ActiveWorkbook.Worksheets
    
      
        Call InsertSUBTOTAL
    
    
    Next ws

End Sub
Sub InsertSUBTOTAL()

Dim LR As Long
Dim ws As Worksheet
Set subs = Range("Grandtotal")
LR = ActiveSheet.Range("J" & Rows.Count).End(xlUp).Row

subs.Copy

ActiveSheet.Range("A" & LR + 1).Select
 
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False

Range("H" & LR + 1).Formula = "=SUM(H7:H" & LR & ")"
Range("I" & LR + 1).Formula = "=SUM(I7:I" & LR & ")"
Range("J" & LR + 1).Formula = "=SUM(J7:J" & LR & ")"
Range("K" & LR + 1).Formula = "=SUM(K7:K" & LR & ")"
Range("M" & LR + 1).Formula = "=SUM(M7:M" & LR & ")"
Range("N" & LR + 1).Formula = "=SUM(N7:N" & LR & ")"
Range("P" & LR + 1).Formula = "=SUM(P7:P" & LR & ")"

Columns.AutoFit
            Columns("L").ColumnWidth = 1.57
            Columns("A").ColumnWidth = 5.29
            Application.CutCopyMode = False
          
              
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Your first sub loops through each of the worksheets, but never actually changes the activesheet.

Code:
Sub InsertSUBTOTAL()

Dim subs As Range
Dim LR As Long
Dim ws As Worksheet


Set subs = Range("Grandtotal")
For Each ws In ActiveWorkbook.Worksheets
    With ws
        LR = .Range("J" & Rows.Count).End(xlUp).Row
        subs.Copy .Range("A" & LR + 1)
        .Range("H" & LR + 1).Formula = "=SUM(H7:H" & LR & ")"
        .Range("I" & LR + 1).Formula = "=SUM(I7:I" & LR & ")"
        .Range("J" & LR + 1).Formula = "=SUM(J7:J" & LR & ")"
        .Range("K" & LR + 1).Formula = "=SUM(K7:K" & LR & ")"
        .Range("M" & LR + 1).Formula = "=SUM(M7:M" & LR & ")"
        .Range("N" & LR + 1).Formula = "=SUM(N7:N" & LR & ")"
        .Range("P" & LR + 1).Formula = "=SUM(P7:P" & LR & ")"
        .Columns.AutoFit
        .Columns("L").ColumnWidth = 1.57
        .Columns("A").ColumnWidth = 5.29
        Application.CutCopyMode = False
    End With
Next ws
              
End Sub
 
Upvote 0
Your revision worked flawlessly! thank you for all your help!

Your first sub loops through each of the worksheets, but never actually changes the activesheet.

Code:
Sub InsertSUBTOTAL()

Dim subs As Range
Dim LR As Long
Dim ws As Worksheet


Set subs = Range("Grandtotal")
For Each ws In ActiveWorkbook.Worksheets
    With ws
        LR = .Range("J" & Rows.Count).End(xlUp).Row
        subs.Copy .Range("A" & LR + 1)
        .Range("H" & LR + 1).Formula = "=SUM(H7:H" & LR & ")"
        .Range("I" & LR + 1).Formula = "=SUM(I7:I" & LR & ")"
        .Range("J" & LR + 1).Formula = "=SUM(J7:J" & LR & ")"
        .Range("K" & LR + 1).Formula = "=SUM(K7:K" & LR & ")"
        .Range("M" & LR + 1).Formula = "=SUM(M7:M" & LR & ")"
        .Range("N" & LR + 1).Formula = "=SUM(N7:N" & LR & ")"
        .Range("P" & LR + 1).Formula = "=SUM(P7:P" & LR & ")"
        .Columns.AutoFit
        .Columns("L").ColumnWidth = 1.57
        .Columns("A").ColumnWidth = 5.29
        Application.CutCopyMode = False
    End With
Next ws
              
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,779
Members
449,049
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top