Sandhu Wheels
Sandhu Wheels

Reputation: 1

How to SUM Totals At Bottom of a Fixed Column in every worksheet vba

How to SUM Total Values from H Column At Bottom of Column H Dynamically on every worksheet. My code are below:

    Sub Sum_All()
    Dim ws As Worksheet
    Application.ScreenUpdating = False
    For Each ws In Worksheets
        ws.Select
        Call Sum_This
    Next
    Application.ScreenUpdating = True
    End Sub

    Sub Sum_This()
    Dim LastRow As Long
    With ActiveSheet
    LastRow = Cells(Rows.Count, "H").End(xlUp).Row
    Range("H" & LastRow + 1).Formula = "=SUM(H2:H" & LastRow & ")"
    End With
    End Sub

I am trying this code but nothing happen.

Please help.

Upvotes: 0

Views: 373

Answers (1)

romulax14
romulax14

Reputation: 555

There are several issues with your code.
You maybe need to check on how to write a With statement. Also, when using For Each to loop through all WorkSheets in a given WorkBook, you need to use this syntax : For Each ws In myWorkBook.Worksheets, myWorkBook being a variable reffering to the WorkBook you want.

Try this :

Sub Sum_All()
    Dim LastRow As Long
    Application.ScreenUpdating = False
    For Each ws In ThisWorkbook.Worksheets
        LastRow = ws.Cells(Rows.Count, "H").End(xlUp).Row
        ws.Range("H" & LastRow + 1).Formula = "=SUM(H2:H" & LastRow & ")"
    Next
    Application.ScreenUpdating = True
End Sub

Upvotes: 2

Related Questions