Question related to AutomateSum

Iron Contributor

Hello Everyone,

I have written VBA code which is showing the AutomateSum. 

 

1st question --- I want to show TOTAL SALES where total calculation has come in all sheet. Like - 

Screenshot (2427).png

 

2nd question --- I hide the NORTH RECORDS sheet, so when i run this code. So it gives error - 

Screenshot (2428).png

 

I click in DEBUG button, then it highlighted this line - 

Screenshot (2429).png

 

Please help.

 

Here is the attached file.

2 Replies

@Excel 

If you want to skip hidden sheets:

Public Sub AutomateSum()
    Dim wsh As Worksheet
    Dim LastRow As Long
    Application.ScreenUpdating = False
    For Each wsh In Worksheets
        If wsh.Visible = xlSheetVisible Then
            LastRow = wsh.Range("F1").End(xlDown).Row
            wsh.Range("A" & LastRow + 1).Value = "TOTAL SALES"
            wsh.Range("F" & LastRow + 1).Formula = "=SUM(F2:F" & LastRow & ")"
        End If
    Next wsh
    Application.ScreenUpdating = True
End Sub

If you want to include hidden sheets:

Public Sub AutomateSum()
    Dim wsh As Worksheet
    Dim LastRow As Long
    Application.ScreenUpdating = False
    For Each wsh In Worksheets
        LastRow = wsh.Range("F1").End(xlDown).Row
        wsh.Range("A" & LastRow + 1).Value = "TOTAL SALES"
        wsh.Range("F" & LastRow + 1).Formula = "=SUM(F2:F" & LastRow & ")"
    Next wsh
    Application.ScreenUpdating = True
End Sub

This code doesn't select cells, so it works for hidden sheets as well as for visible sheets.

It worked! Thank you so much sir:smiling_face_with_smiling_eyes: