# Loop Formula Across Columns and Down Rows

#### suremac

##### New Member
Greetings,

I'd like to loop a formula across columns and have it stop when it gets to the last column that I have defined, while also looping the formula down rows to the last row that I have defined in the respective columns. I think I have a rough idea of how some things should be defined but not a good idea of the most efficient loop to use or how the formula should be formatted. It should be noted that, in the 12th line of code, the starting row is variable. I’m open to using R1C1 notation if necessary to reformat the formula but I do not understand it very well.

Code:
``````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(--(B6:B7>=\$A20),--(B6:B7<=(A20+30))*B4)”
Next R
Next C
End Sub``````

Any help is much appreciated.

### Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

#### suremac

##### New Member
Here is the formula with the references:
Code:
``SUMPRODUCT(--(C\$6:C\$7>=\$A20),--(C\$6:C\$7<=(\$A20+30))*C\$4)``

#### suremac

##### New Member
To whoever comes across this thread, this is how to loop a simple formula using R1C1 notation across columns and rows.

Code:
``````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`````` Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

### Forum statistics

1,164,381
Messages
5,836,922
Members
430,463
Latest member
mikmob ### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?    1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option. Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com". Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button. Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button. Go back