Macro to add sum to all worksheets in a workbook except for a certain worksheets

feni1388

Board Regular
Joined
Feb 19, 2018
Messages
133
Office Version
  1. 2021
Platform
  1. Windows
Hello

I have a macro that split a master data into several worksheets.
The number of worksheets changes every months.
I need to add the sum total of sales that will be in column G. The number of rows will also changes, sometime only one row.
There are 3 sheets that have different format and there's no need to add sum to these 3 sheets.

So I need a macro that will loop through all the sheets within the workbook, except for those 3, and add the sum in the end of column G.

Sub Addingsumtoeachsheet()

Dim sh As Worksheet
Dim I As Integer
Dim Ws_Number As Integer
Dim SumRg As Range

For Each sh In ActiveWorkbook.Worksheets
Select Case sh.Name
Case Is = "Unique", "Sales Report", "Sales details"

Case Else
With sh
Set SumRg = ActiveSheet.Range("G2", Range("G2").End(xlDown).End(xlToRight))
Range("G2").End(xlDown).Offset(1, 0) = WorksheetFunction.Sum(SumRg)
Columns("G:G").Select
Selection.Style = "Comma [0]"
End With
End Select
Next sh

End Sub

I tried the above macro code and it seems that excluding those 3 worksheets works, but not with adding the sum to each worksheets.

can someone please help?

Thank you in advance.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Do you want to finish up with the Sum formula in the spreadsheets or just the result of the sum ?
You are currently using the worksheet function to insert just the value and not the formula. This seems an unlikely intention. In the below I have provided both options.

VBA Code:
Sub Addingsumtoeachsheet_mod()

    Dim sh As Worksheet
    Dim I As Integer
    Dim Ws_Number As Integer
    Dim SumRg As Range
    
    For Each sh In ActiveWorkbook.Worksheets
        Select Case sh.Name
        Case Is = "Unique", "Sales Report", "Sales details"
        
        Case Else
            With sh
                Set SumRg = .Range("G2", .Range("G" & Rows.Count).End(xlUp))
                ' This puts the Sum formula into the spreadsheet
                .Range("G" & Rows.Count).End(xlUp).Offset(1, 0) = "=Sum(" & SumRg.Address & ")"
                ' Comment out the line above and Uncomment the next line if you just want the result of the Sum
                '.Range("G" & Rows.Count).End(xlUp).Offset(1, 0) = WorksheetFunction.Sum(SumRg)
                .Columns("G:G").Style = "Comma [0]"
            End With
        End Select
    Next sh

End Sub
 
Upvote 1
Solution
Actually I want to finish up with the Sum formula.
Thank you. It works as I expected.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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