为 D - M 列添加总计行

Add Total Row For Columns D - M

我一直在使用此语法为 D 列添加总行 - 但现在我需要为 D - M 添加总行。与其多次编写此代码并更改列名,不如有一个可将总行添加到列 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)"

这与突出显示该范围并按 Ctrl+右箭头具有相同的效果

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