Subtotal column on all worksheets

kellexlsx

New Member
Joined
Oct 9, 2017
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hi - I have a workbook with multiple worksheets. All have the same columns, but varying numbers of rows. Is there a simple macro that will give me a bolded total in one particular column - let's say column A, for simplicity - that is one row below the last line of data, and then repeat that process on each successive worksheet?

I've always gotten great advice from the people on this forum, and I hope you all know how much we newbies appreciate your talent and generosity!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
There are two ways I wonder consider:

1. Use VBA to automatically add your total rows to the bottom of each sheet

- or -

2. Move the total row to the top, to row 1.
Then in your formula, you can start from row 2 and choose some ending row number that you know that you will never exceed, i.e.
Excel Formula:
=SUM(A2:A10000)
 
Upvote 0
Doing a total row up top is something I hadn't considered, but people in my company go a little bonkers when you change the format of a report on them, and they're used to seeing it at the bottom. So VBA would be preferable, but I'm pretty useless beyond recording simple macros.

Thank you for your suggestion!
 
Upvote 0
OK, which columns do you want to apply it to? Is it only column A, or other columns too?
Is it a simple SUM formula, or a different formula (if different, please post the formula)?
Does the first row of actual data to consider start on row 1, 2, or some other row?
 
Upvote 0
In this particular case, I need the sum of column AD, bolded and (I forgot to add) in Accounting format. There is a single, identical header row on each worksheet.
 
Upvote 0
OK, try this:
VBA Code:
Sub AddTotals()

    Dim ws As Worksheet
    Dim lr As Long
    
'   Loop through each sheet
    For Each ws In ActiveWorkbook.Worksheets
        With ws
'           Find last row with data in column AD
            lr = .Cells(.Rows.Count, "AD").End(xlUp).Row
'           Add total row
            .Cells(lr + 1, "AD").Formula = "=SUM(AD2:AD" & lr & ")"
        End With
    Next ws
    
End Sub
 
Upvote 0
Thank you so much for the notes, it makes it so much easier to follow along! This works great - is there an extra line I can add to make the cell bold? I tried

ActiveCell.Font.Bold = True

but it didn't like that.
 
Upvote 0
Thank you so much for the notes, it makes it so much easier to follow along! This works great - is there an extra line I can add to make the cell bold? I tried

ActiveCell.Font.Bold = True

but it didn't like that.
You already have the address of the cell you need here:
VBA Code:
.Cells(lr + 1, "AD")
so just add another row like:
VBA Code:
Sub AddTotals()

    Dim ws As Worksheet
    Dim lr As Long
    
'   Loop through each sheet
    For Each ws In ActiveWorkbook.Worksheets
        With ws
'           Find last row with data in column AD
            lr = .Cells(.Rows.Count, "AD").End(xlUp).Row
'           Add total row
            .Cells(lr + 1, "AD").Formula = "=SUM(AD2:AD" & lr & ")"
'           Make cell bold
            .Cells(lr + 1, "AD").Font.Bold = True
        End With
    Next ws
    
End Sub
 
Upvote 0
Solution
Perfect. Thank you so much for your help and your patience. You totally made my day.
 
Upvote 0
You are welcome.
Glad I was able to help!
:)
 
Upvote 0

Forum statistics

Threads
1,215,559
Messages
6,125,517
Members
449,236
Latest member
Afua

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