Changing formula range when looping in VBA

DSCHROE67

New Member
Joined
Sep 22, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am trying to create a loop that will increment the column value in the formula below to to the right each time the loop continues.
In the formula below C4 needs to become C5, C6,... etc.
The formula works when I loop it but it is always providing the subtotal for column 4

ActiveCell.FormulaR1C1 = "=SUBTOTAL(9,(OFFSET(R2C4,0,0,COUNTA(C1)-1,1)))"


Below is the code I was trying to use. I was trying to define an amount (AAA) and add 1 to it. The AAA would replace the column reference (starting at 4) in the bold formula below

Sub macro ()
Dim AAA As Integer
AAA = 4

Range("D1").Select
Selection.End(xlDown).Select

Do While ActiveCell <> ""
ActiveCell.Offset(2, 0).Select
ActiveCell.FormulaR1C1 = "=SUBTOTAL(9,(OFFSET(R2CAAA,0,0,COUNTA(C1)-1,1)))"
ActiveCell.Offset(-2, 1).Select
AAA=AAA+1
Loop

End Sub

Thanks for any input.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Since the formula is a string, you just concatenate using &:
Rich (BB code):
ActiveCell.FormulaR1C1 = "=SUBTOTAL(9,(OFFSET(R2C" & AAA & ",0,0,COUNTA(C1)-1,1)))"
 
Upvote 0
Solution
Thanks so much for your help. This worked perfect. I new I was on the right track just couldn't remember the correct syntax
 
Upvote 0

Forum statistics

Threads
1,215,767
Messages
6,126,773
Members
449,336
Latest member
p17tootie

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
Back
Top