Search code examples
vbaexcelexcel-2013

Add Total Row For Columns D - M


I have been using this syntax to add a total row for column D - but now I need to add the total row for D - M. Instead of just writing this code multiple times and changing the column name is there a quick re-usable function that could add a total row to the bottom of columns D - M?

With ActiveSheet
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
    lastrow = .Cells.Find(What:="*", _
                  After:=.Range("D1"), _
                  Lookat:=xlPart, _
                  LookIn:=xlFormulas, _
                  SearchOrder:=xlByRows, _
                  SearchDirection:=xlPrevious, _
                  MatchCase:=False).Row
Else
    lastrow = 1
End If
End With
Range("D" & lastrow +1).FormulaR1C1 = "=SUM(R[-" & lastrow & "]C:R[-1]C)"

Solution

  • This has the same effect as highlighting that range and hitting Ctrl+Right Arrow

    range("D" & lastrow + 1 & ":M" & lastrow + 1).FillRight