Thread: How to Loop
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Bernie Deitrick Bernie Deitrick is offline
external usenet poster
 
Posts: 5,441
Default How to Loop

DTTODGG,

No need to loop - the formulas that you are using are the same in RC, so get
rid of the absolute addressing and enter it into all the cells at once:

Range("C2:D4").FormulaR1C1 = "=SUM(R[15]C,R[30]C,R[45]C,R[61]C)"

So, to enter your formulas in every column from C to Z:
Range("C2:Z4").FormulaR1C1 = "=SUM(R[15]C,R[30]C,R[45]C,R[61]C)"

And, by the way, your formula does NOT sum every fifteen row - the last cell
is off by one row from that pattern. If the pattern held, you could use a
different formula....

By the way, if you wanted to loop:

Sub Macro1()
Dim i As Integer
Dim j As Integer

For i = 2 To 4
For j = 3 To 26
Cells(i, j).FormulaR1C1 = "=SUM(R" & i + 15 & "C" & j & _
",R" & i + 30 & "C" & j & ",R" & i + 45 &
"C" & j & _
",R" & i + 61 & "C" & j & ")"
Next j
Next i
End Sub

HTH,
Bernie
MS Excel MVP


"DTTODGG" wrote in message
...
Hello,
I have to run totals on many rows/columns.
I started a little macro, but would like to learn how to loop thru this
data.

Please explain as you go, so I can learn rather than just copy your
excellent code :-)

'Calculate Totals for every 15th row (3 in sample, 8 rows total)

Range("C2").Select
ActiveCell.FormulaR1C1 = "=SUM(R17C3,R32C3,R47C3,R63C3)"

Range("C3").Select
ActiveCell.FormulaR1C1 = "=SUM(R18C3,R33C3,R48C3,R64C3)"

Range("C4").Select
ActiveCell.FormulaR1C1 = "=SUM(R19C3,R34C3,R49C3,R65C3)"

'Move to next Column and calculate (2 in sample, 6 columns total)

Range("D2").Select
ActiveCell.FormulaR1C1 = "=SUM(R17C4,R32C4,R47C4,R63C4)"

Range("D3").Select
ActiveCell.FormulaR1C1 = "=SUM(R18C4,R33C4,R48C4,R64C4)"

Range("D4").Select
ActiveCell.FormulaR1C1 = "=SUM(R19C4,R34C4,R49C4,R65C4)"

Thanks in advance!