我有一个问题,写VBA的自动和,其中列可能增加或减少不时。以下面为例。我已将LastCol设置为查找最后一列,然后从行的B列自动求和到最后一列,以获得“总计”。我希望尽可能避免使用R1C1公式。此外,RC-4将根据电子表格上的列数进行更改。
Sub AutoSum()
Dim LastCol As Integer
Sheets("Sheet1").Select
LastCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Cells(2, LastCol1 + 1).Select
ActiveCell.FormulaR1C1 = "=SUBTOTAL(9,RC[-4]: RC[-1])"
End Sub发布于 2016-03-14 14:54:58
试试看:
Sub AutoSum()
Dim LastCol As Integer
With Sheets("Sheet1")
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
.Cells(2, LastCol1 + 1).Formula = "=SUBTOTAL(9,Offset(B2,0,0,1," & LastCol-1 & "))"
End With
End Sub运行以上代码后:

https://stackoverflow.com/questions/35990714
复制相似问题