Copying formula in variable length column

awowen

New Member
Joined
Mar 18, 2002
Messages
1
I need to copy a formula in a variable length column via a macro.
I am using the following:
Range("I2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-2]"
Selection.AutoFill Destination:=Range("I2:I6264"), Type:=xlFillDefault
Range("I2:I6264").Select
Obviously, this is not acceptable for variable lengths.
What can I do to accomplish?
Thanks,
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try the following code:

Rowcounter = Intersect(ActiveSheet.UsedRange, Columns("I")).Rows.Count
For i = 2 To Rowcounter
Range("I" & i).FormulaR1C1 = "=RC[-1]-RC[-2]"
Next

The code places your formula in all cells from I2 to the end of the column.
 
Upvote 0
On 2002-03-19 12:25, Al Chara wrote:
Try the following code:

Rowcounter = Intersect(ActiveSheet.UsedRange, Columns("I")).Rows.Count
For i = 2 To Rowcounter
Range("I" & i).FormulaR1C1 = "=RC[-1]-RC[-2]"
Next

The code places your formula in all cells from I2 to the end of the column.

Ouch... that loop just hurt me ! :wink:

Try this one:

Range("I2").Resize(Rowcounter,1).FormulaR1C1 = "=RC[-1]-RC[-2]"
 
Upvote 0
Just getting in on the act, this might work also, and it will handle non-contiguous data in column H (which you could adjust for G which if you do, change the offset column reference from 1 to 2.).

Sub FormulaFun()
Range([H2], [H65536].End(xlUp)).Offset(0, 1).Formula = "=RC[-1]-RC[-2]"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,634
Messages
6,120,659
Members
448,975
Latest member
sweeberry

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