我想在列之间循环一个公式并让它在到达我定义的最后一列时停止,同时还将公式向下循环到我在相应列中定义的最后一行。我想我已经大致了解了应该如何定义某些内容,但不是对使用最有效的循环或如何格式化公式的好主意。应该注意,在第12行代码中,起始行是可变的。如果有必要重新格式化公式,我愿意使用R1C1表示法,但我不太了解它。
Sub LoopAcrossColsRows()
Dim C as Integer
Dim R as Integer
Dim LastCol As Integer
Dim LastRow As Long
Dim lr As Long
Worksheets("Sheet1").Activate
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
LastRow = Cells(Rows.Count, 20).End(xlUp).Row
lr = Cells(Rows.Count, 1).End(xlUp).Row
For C = 2 To Last Col
For R = lr + 2 To LastRow
Cells(C, R) = “SUMPRODUCT(--(C$6:C$7>=$A20),--(C$6:C$7<=($A20+30))*C$4)”
Next R
Next C
End Sub
非常感谢任何帮助。
答案 0 :(得分:1)
为什么在这种情况下循环?
Range(cells(2,3),cells(lr,lc)).FormulaR1C1 = "=SUM(RC[-1])"
应该做得更快!
答案 1 :(得分:0)
对于遇到此线程的人来说,这是如何使用R1C1表示法跨列和行循环一个简单的公式。
Sub LoopAcrossColsRows()
Dim c As Long, lc As Long, r As Long, lr As Long
With Sheets("Sheet3")
lc = .Cells(1, Columns.Count).End(xlToLeft).Column
lr = Cells(Rows.Count, 1).End(xlUp).Row
For c = 3 To lc
For r = 2 To lr
.Cells(r, c).FormulaR1C1 = "=SUM(RC[-1])"
Next r
Next c
End With
End Sub