VBA to add subtotals to each sheet in my workbook

BORUCH

Well-known Member
Joined
Mar 1, 2016
Messages
528
Office Version
  1. 365
Platform
  1. Windows
Hi
I have a lot of sheets in my workbook I would like an excel vba that would add subtotal
It should fInd the last empty row on each sheet and on column a write "subtotal" and then subtotal columns E ,F,G,H,I

It should also format columns G subtotal as numbers with a comma format

Thanks
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Please, try following code:

VBA Code:
Sub Subtotals()

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets

    lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
   
    ws.Cells(lastrow + 1, "A").Value = "Subtotal"
   
    ws.Cells(lastrow + 1, "E").Formula = "=SUBTOTAL(9,E2:E" & lastrow & ")"
    ws.Cells(lastrow + 1, "F").Formula = "=SUBTOTAL(9,F2:F" & lastrow & ")"
    ws.Cells(lastrow + 1, "G").Formula = "=SUBTOTAL(9,G2:G" & lastrow & ")"
    ws.Cells(lastrow + 1, "H").Formula = "=SUBTOTAL(9,H2:H" & lastrow & ")"
    ws.Cells(lastrow + 1, "I").Formula = "=SUBTOTAL(9,I2:I" & lastrow & ")"
   
    ws.Cells(lastrow + 1, "G").NumberFormat = "#,##0"
   
Next ws

End Sub
 
Upvote 0
Please, try following code:

VBA Code:
Sub Subtotals()

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets

    lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
  
    ws.Cells(lastrow + 1, "A").Value = "Subtotal"
  
    ws.Cells(lastrow + 1, "E").Formula = "=SUBTOTAL(9,E2:E" & lastrow & ")"
    ws.Cells(lastrow + 1, "F").Formula = "=SUBTOTAL(9,F2:F" & lastrow & ")"
    ws.Cells(lastrow + 1, "G").Formula = "=SUBTOTAL(9,G2:G" & lastrow & ")"
    ws.Cells(lastrow + 1, "H").Formula = "=SUBTOTAL(9,H2:H" & lastrow & ")"
    ws.Cells(lastrow + 1, "I").Formula = "=SUBTOTAL(9,I2:I" & lastrow & ")"
  
    ws.Cells(lastrow + 1, "G").NumberFormat = "#,##0"
  
Next ws

End Sub
Thanks

can you add to this that it should center that subtotal line on every sheet, and put borders on it and auto fit it
 
Upvote 0
Sure.

VBA Code:
Sub Subtotals()

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets

    lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
   
    ws.Cells(lastrow + 1, "A").Value = "Subtotal"
   
    ws.Cells(lastrow + 1, "E").Formula = "=SUBTOTAL(9,E2:E" & lastrow & ")"
    ws.Cells(lastrow + 1, "F").Formula = "=SUBTOTAL(9,F2:F" & lastrow & ")"
    ws.Cells(lastrow + 1, "G").Formula = "=SUBTOTAL(9,G2:G" & lastrow & ")"
    ws.Cells(lastrow + 1, "H").Formula = "=SUBTOTAL(9,H2:H" & lastrow & ")"
    ws.Cells(lastrow + 1, "I").Formula = "=SUBTOTAL(9,I2:I" & lastrow & ")"
   
    ws.Cells(lastrow + 1, "G").NumberFormat = "#,##0"

    ws.Range("A" & lastRow + 1 & ":I" & lastRow + 1).HorizontalAlignment = xlCenter
    
    ws.Range("A" & lastRow + 1 & ":I" & lastRow + 1).Borders(xlEdgeTop).LineStyle = xlContinuous
    ws.Range("A" & lastRow + 1 & ":I" & lastRow + 1).Borders(xlEdgeBottom).LineStyle = xlContinuous
    ws.Range("A" & lastRow + 1 & ":I" & lastRow + 1).Borders(xlEdgeLeft).LineStyle = xlContinuous
    ws.Range("A" & lastRow + 1 & ":I" & lastRow + 1).Borders(xlEdgeRight).LineStyle = xlContinuous
    
    ws.Range("A" & lastRow + 1 & ":I" & lastRow + 1).EntireRow.AutoFit
   
Next ws

End Sub
 
Upvote 0
Sure.

VBA Code:
Sub Subtotals()

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets

    lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
  
    ws.Cells(lastrow + 1, "A").Value = "Subtotal"
  
    ws.Cells(lastrow + 1, "E").Formula = "=SUBTOTAL(9,E2:E" & lastrow & ")"
    ws.Cells(lastrow + 1, "F").Formula = "=SUBTOTAL(9,F2:F" & lastrow & ")"
    ws.Cells(lastrow + 1, "G").Formula = "=SUBTOTAL(9,G2:G" & lastrow & ")"
    ws.Cells(lastrow + 1, "H").Formula = "=SUBTOTAL(9,H2:H" & lastrow & ")"
    ws.Cells(lastrow + 1, "I").Formula = "=SUBTOTAL(9,I2:I" & lastrow & ")"
  
    ws.Cells(lastrow + 1, "G").NumberFormat = "#,##0"

    ws.Range("A" & lastRow + 1 & ":I" & lastRow + 1).HorizontalAlignment = xlCenter
   
    ws.Range("A" & lastRow + 1 & ":I" & lastRow + 1).Borders(xlEdgeTop).LineStyle = xlContinuous
    ws.Range("A" & lastRow + 1 & ":I" & lastRow + 1).Borders(xlEdgeBottom).LineStyle = xlContinuous
    ws.Range("A" & lastRow + 1 & ":I" & lastRow + 1).Borders(xlEdgeLeft).LineStyle = xlContinuous
    ws.Range("A" & lastRow + 1 & ":I" & lastRow + 1).Borders(xlEdgeRight).LineStyle = xlContinuous
   
    ws.Range("A" & lastRow + 1 & ":I" & lastRow + 1).EntireRow.AutoFit
  
Next ws

End Sub
Thanks that worked
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,731
Members
448,987
Latest member
marion_davis

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