Loop Formula Across Columns and Down Rows

suremac

New Member
Joined
Jan 27, 2014
Messages
49
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.
 

Some videos you may like

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

suremac

New Member
Joined
Jan 27, 2014
Messages
49
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
Joined
Jan 27, 2014
Messages
49
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
 

Watch MrExcel Video

Forum statistics

Threads
1,123,322
Messages
5,600,951
Members
414,417
Latest member
Nobu

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.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

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

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

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
Top