![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
New Member
Join Date: Mar 2002
Posts: 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, |
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
|
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.
__________________
Kind regards, Al Chara |
|
|
|
|
|
#3 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
|
Quote:
Try this one: Range("I2").Resize(Rowcounter,1).FormulaR1C1 = "=RC[-1]-RC[-2]" |
|
|
|
|
|
|
#4 |
|
MrExcel MVP
Join Date: Feb 2002
Location: San Francisco, California USA
Posts: 10,387
|
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 |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|