VBA with two loops

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

VBA with two loops

Post by gailb »

I have this working okay, but can't figure out the right syntax to make a second loop.

If possible, how can I consolidate the formula step down to one line? Right now, with each loop, the code adds three formulas.

Code: Select all

Sub Macro1()
    Dim i As Long
    For i = 1 To 12 Step 5
        Cells(i + 1, 3).FormulaR1C1 = "=SUMIFS(C[14],C[12],R" & i & "C5,C[13],RC[-1])"
        Cells(i + 2, 3).FormulaR1C1 = "=SUMIFS(C[14],C[12],R" & i & "C5,C[13],RC[-1])"
        Cells(i + 3, 3).FormulaR1C1 = "=SUMIFS(C[14],C[12],R" & i & "C5,C[13],RC[-1])"
    Next i
End Sub

gailb
3StarLounger
Posts: 254
Joined: 09 May 2020, 14:00

Re: VBA with two loops

Post by gailb »

Sorry, I just realized the answer as I was posting. Resize.

Code: Select all

Cells(i + 1, 3).Resize(3).FormulaR1C1 = "=SUMIFS(C[14],C[12],R" & i & "C5,C[13],RC[-1])
"
Last edited by gailb on 27 Sep 2021, 15:56, edited 1 time in total.

User avatar
HansV
Administrator
Posts: 78457
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA with two loops

Post by HansV »

:thumbup:
Best wishes,
Hans