Autofill a dynamic range that consist of two variables

HJA14

Board Regular
Joined
Apr 12, 2016
Messages
60
Dear all,

I have a cell range that changes continuously. Therefore, both cells (begin- and end cell) are variable and not static. I would like to make sure that the cells in this rang have the "same formula". This formula has A1 (not R1C1)-notation and consist of multiple cell references.

Code:
[/COLOR][SIZE=2]Range("C" & LastRowOld).Select
[/SIZE][/FONT][FONT=arial][SIZE=2]With Range("C" & LastRowOld)[/SIZE][/FONT]
[FONT=arial][SIZE=2]    .Formula = "myFormula"[/SIZE][/FONT]
[FONT=arial][SIZE=2]    .AutoFill Destination:=Range("C" & LastRowOld & ":C" & LastRowNew)[/SIZE]
[/FONT]


Earlier in my VBA-macro I declared the variables LastRowNew and LastRowOld.
I get the following error message Autofill method of Range classes failed.

I know that if the start cell would have been C2 (static) the following macro would have done the job:

Code:
[/COLOR][SIZE=2]Range("C2").Select
[/SIZE][/FONT][FONT=arial][SIZE=2]With Range("C2")[/SIZE][/FONT]
[FONT=arial][SIZE=2]    .Formula = "myFormula"[/SIZE][/FONT]
[FONT=arial][SIZE=2]    .AutoFill Destination:=Range("C2 & C" & LastRowNew)[/SIZE]
[/FONT]


How do I write the
.AutoFill Destination:=Range("C" & LastRowOld & ":C" & LastRowNew) correctly?

Thanks in advance,


 
Last edited:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Range("C" & LastRowOld & ":C" & LastRowNew) .Formula = "myFormula"
 
Last edited:
Upvote 0
Range("C" & LastRowOld & ":C" & LastRowNew) .Formula = "myFormula"

Thank you for taking the time to help me with my problem. The problem with your solution is that the formula in the cells are not based on R1C1-notation. For example the formula in cell "C" & LastRowOld is
Code:
.Formula = "=INDEX(Sheet1!D:D,MATCH(Data!B & LastRowOld,Sheet1!C:C,0))"

So, I need the macro to autofill this formula with the correct cell reference in the other cells of the range. Any suggestions?
 
Upvote 0
I can't see why this shouldn't work :

Range("C" & LastRowOld & ":C" & LastRowNew) .Formula
= "=INDEX(Sheet1!D:D,MATCH(Data!B" & LastRowOld & ",Sheet1!C:C,0))"
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

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