VBA Code to Fill a Dynamic Range

mniesse

New Member
Joined
Mar 13, 2020
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am trying to fill down to a dynamic row number that is calculated in another sheet. When I use the recorder I get this but I need it to fill down from A2:x within the current sheet (this is the dynamic field that needs to point to the row number to end on such as X1160). What is the code to accomplish this?

Range(Selection, Selection.End(xlToRight)).Select
'The cell in bold needs to be updated to X & the number in a different sheet called "Instruction" calculated in B2 of that sheet
Selection.AutoFill Destination:=Range("A2:X1060"), Type:=xlFillDefault
Range("A2:X1060").Select

Does this make sense? Any help is appreciated.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try:
VBA Code:
Sub test()
    Dim LastRow As Long
    LastRow = Sheets("Instruction").Range("B2").Value
    Range("A2:X2").AutoFill Destination:=Range("A2:X" & LastRow), Type:=xlFillDefault
End Sub
 
Upvote 0
Thank you. That worked when I ran the macro in the "Instructions" worksheet but need it to copy the range down in the worksheet called "Import". Any help updating the code so fill range copies down in the correct sheet?
 
Upvote 0
Try:
VBA Code:
Sub test()
    Dim LastRow As Long
    LastRow = Sheets("Instruction").Range("B2").Value
    Sheets("Import").Range("A2:X2").AutoFill Destination:=Sheets("Import").Range("A2:X" & LastRow), Type:=xlFillDefault
End Sub
 
Upvote 0
Disregard. I figured it out by putting Sheet1 and Sheet3 in front of the specific code to complete the goal. Thank you again for your help!
 
Upvote 0

Forum statistics

Threads
1,214,946
Messages
6,122,401
Members
449,081
Latest member
JAMES KECULAH

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