VBA怎么求逐行累积
有千行效果如:
第二列 第三列
1 1
2 3
3 6
4 10
5 15
[解决办法]
- VB code
Dim strRange As String Dim nRow As Integer nRow = 1000 '自己判断看要计算多少行 自己赋值 strRange = "C2:C" & nRow Range("C2").Select ActiveCell.FormulaR1C1 = "=R[-1]C+RC[-1]" Range("C2").Select Selection.AutoFill Destination:=Range(strRange), Type:=xlFillDefault
[解决办法]
[解决办法]
简单,看这个:
- VB code
Sub Main() '填20行数据 Call SetFormula(20)End SubSub SetFormula(ByVal n&)' 入口参数:n ----- 到第几行截止' *** 从第一行开始,填到 B 、C 两列 ***' 要填到其它列,自己参考修改 Dim i& Cells(1, 2).Formula = 1 Cells(1, 3).Formula = 1 For i = 2 To n Cells(i, 2).Formula = "=R[-1]C+1" Cells(i, 3).Formula = "=RC[-1]+R[-1]C" NextEnd Sub
[解决办法]
[解决办法]
先填充最后一个单元格,反过来自动填充就可以了 (G16:G22)
Range("G22").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-6]C[-1]:RC[-1])"
Range("G22").Select
Selection.AutoFill Destination:=Range("G16:G22"), Type:=xlFillDefault
Range("G16:G22").Select