Drag Formula Based on Dynamic Range

joanna_sjw

New Member
Joined
Apr 28, 2020
Messages
8
Office Version
  1. 2013
Platform
  1. Windows
Hi! I'm facing an error for autofill formula. The formula is found in the last row of column Q to Y (e.g. 100). But the thing is, the last row is dynamic hence I have also name the lastrows accordingly so it will change whenever the macro is ran.
The formula has to be dragged to the last row of column A (e.g. 200). The last row of column A is also dynamic as i have datasets pasted in daily.
Not too sure if anyone is able to help me with this line which the debugger is showing me where the error is.

Selection.AutoFill Destination = sht.Range("Q" & lastrow, Range("Y" & lastrow2))

Below is the code that I currently have.

VBA Code:
Sub Drag_Formulas()
Dim lastrow As Long
Dim lastrow2 As Long
Dim StartCell As Range
Dim EndCell As Range
Dim sht As Worksheet

Set sht = ThisWorkbook.Worksheets("DATA")
lastrow = sht.Range("Q" & Rows.Count).End(xlUp).Row
lastrow2 = sht.Range("A" & Rows.Count).End(xlUp).Row
Set StartCell = sht.Range("Q" & lastrow)
Set EndCell = sht.Range("Y" & lastrow2)

Range("Q1").Select
Selection.End(xlDown).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.AutoFill Destination = sht.Range("Q" & lastrow, Range("Y" & lastrow2))
Range(Selection, Selection.End(xlDown)).Select

End Sub

Thanks in advance!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Replace
VBA Code:
Range("Q1").Select
Selection.End(xlDown).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.AutoFill Destination = sht.Range("Q" & lastrow, Range("Y" & lastrow2))
Range(Selection, Selection.End(xlDown)).Select
with
VBA Code:
   Range(startcell, endcell).FillDown
 
Upvote 0
Replace
VBA Code:
Range("Q1").Select
Selection.End(xlDown).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.AutoFill Destination = sht.Range("Q" & lastrow, Range("Y" & lastrow2))
Range(Selection, Selection.End(xlDown)).Select
with
VBA Code:
   Range(startcell, endcell).FillDown

Works like a charm! Thank you!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

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