Vba 为D-M列添加总行

Vba 为D-M列添加总行,vba,excel,excel-2013,Vba,Excel,Excel 2013,我一直在使用这种语法为D列添加总行,但现在我需要为D-M添加总行。与其多次编写此代码并更改列名,是否有一个快速可重用的函数可以将总行添加到D-M列的底部 With ActiveSheet If Application.WorksheetFunction.CountA(.Cells) <> 0 Then lastrow = .Cells.Find(What:="*", _ After:=.Range("D1"), _

我一直在使用这种语法为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)"
使用ActiveSheet的

如果Application.WorksheetFunction.CountA(.Cells)为0,则
lastrow=.Cells.Find(内容:=“*”_
之后:=.范围(“D1”)_
看:=xlPart_
LookIn:=xl公式_
搜索顺序:=xlByRows_
搜索方向:=xlPrevious_
MatchCase:=False)。行
其他的
lastrow=1
如果结束
以
范围(“D”&lastrow+1)。公式1c1=“=SUM(R[-”&lastrow&“]C:R[-1]C)”

这与高亮显示该范围并按Ctrl+向右箭头的效果相同

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

很好,得到了我的答案并学习了一个新的VBA函数。非常感谢。