Hello Jluc,
This macro will add the bold total line with the sum formula to each column header (starting at column "M") that matches your headers list. The list is marked in bold. You can add to or remove from the list if you want.
The worksheet checked is "Sheet1". You can change this to another worksheet as well.
Sub Totals()
Dim C As Long
Dim Cell As Range
Dim LastRow As Long
Dim Rng As Range
Dim RngEnd As Range
Dim Wks As Worksheet
Set Wks = Worksheets("Sheet1")
Set Rng = Wks.Range("M1")
Set RngEnd = Wks.Cells(1, Columns.Count).End(xlToLeft)
If RngEnd.Column < Rng.Column Then Exit Sub
Set Rng = Wks.Range(Rng, RngEnd)
For Each Cell In Rng.Columns.Cells
Select Case Cell
Case Is = "Target", "Target1", "Target3"
C = Cell.Column
LastRow = Wks.Cells(Rows.Count, C).End(xlUp).Row
If LastRow > 1 Then
Set Cell = Cell.Resize(LastRow - Cell.Row + 1, 1)
Wks.Cells(LastRow + 1, C).Formula = "=SUM(" & Cell.Address & ")"
Wks.Cells(LastRow + 1, C).Font.Bold = True
End If
End Select
Next Cell
End Sub
Bookmarks