Autofill in VBA

Azar

New Member
Joined
Jun 28, 2011
Messages
28
I know this is fairly common, but my internet searches have not helped me solve the problem.

I have a formula in "E1" and I want my VBA macro to drag or autofill the formula down to the corresponding last cell that has data in Column D.
I tried this:

Range("E1").Select

Selection.AutoFill Destination:=Range("E1:E" & Range("D" & Rows.Count).End(x1UP).Row)

Alas, I get the venerable "run-time error 1004 application defined or object defined error"

any help would be great!

thanks much
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I know this is fairly common, but my internet searches have not helped me solve the problem.

I have a formula in "E1" and I want my VBA macro to drag or autofill the formula down to the corresponding last cell that has data in Column D.
I tried this:

Range("E1").Select

Selection.AutoFill Destination:=Range("E1:E" & Range("D" & Rows.Count).End(x1UP).Row)

Alas, I get the venerable "run-time error 1004 application defined or object defined error"

any help would be great!

thanks much

That should be
xlUp (as in Excel Up)
It's the LETTER L, not the number one.

Hope that helps.
 
Upvote 0
That should be l not 1

Rich (BB code):
Selection.AutoFill Destination:=Range("E1:E" & Range("D" & Rows.Count).End(xlUp).Row)
 
Upvote 0
Hi guys,

I have a similar problem. I've been trying to use either of the following formulas in immediate window:

Range("A2").Copy
Range("B" & Rows.Count).End(xlUp).Offset(0, -1).Select
Range(Selection, Selection.End(xlUp).Offset(1, 0)).PasteSpecial xlPasteFormulas

or

BLR = Range("B" & Rows.Count).End(xlUp).Offset(0, -1).Row
Range("A2").Select
Selection.autofill Destination:=Range("A2:A" & BLR), Type:=xlFillDefault


----

The macro first inserts a column in "A", then inserts a formula in A2 which I want to copy from A3 to the last row. BLR is the last row which is needed to be taken from column B because A is empty and consequently it will choose the last empty row in the sheet.

I have encountered this problem before. I would appreciate your answer and any advices for future how to deal with it.

Cheers,
Slawek
 
Upvote 0
This should work

Code:
BLR = Range("B" & Rows.Count).End(xlUp).Row
Range("A2").AutoFill Destination:=Range("A2:A" & BLR), Type:=xlFillDefault
 
Upvote 0
Thanks for your reply VoG.

I keep getting the same error - 1004.

Do you have any other ideas?
 
Upvote 0
This worked for me in the immediate window:

Code:
Range("A2").AutoFill Destination:=Range("A2:A" & Range("B" & Rows.Count).End(xlUp).Row), Type:=xlFillDefault

as did VOG's code.
 
Upvote 0
Ok, so I have tested your code VoG and I had a problem with MsgBox showing the number of the last row (blank window). I thought there is something wrong with the immediate window.

I incorporated the changes in the macro and run it from the beginning to the end without any errors. The column is appropriately and fully filled with the formulas. In short, it works!

Thanks guys for your help.


PS: Do you know any possible reasons why the immediate window didn't work appropriately? Even after restarting Excel it didn't work correctly.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,999
Members
452,373
Latest member
TimReeks

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