How to drag autofill from a set location a variable amount of cells away

ExcelDoer

New Member
Joined
Aug 13, 2024
Messages
8
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hello everyone! I am hoping someone has figured out this situation as I am having trouble with it.

The Problem:
  • I am given a month from the user
  • I am given how many months out the user wants to autofill from the given month
    • Example: The given month is January, and they want to autofill out 6 months to June
How do I write the autofill function within VBA to include the dynamic column length to which I want to autofill the formula? This is what I have right now but I get errors from it:

VBA Code:
dim x as range
dim numberOfMonths as integer

numberOfMonths = 15
set x = cells(23,numberOfMonths)
Selection.AutoFill Destination:=Range(x), Type:=xlFillDefault
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi ExcelDoer, welcome to the MrExcel Forum. If the begging of your autofil range is your "Selection" and the end of your range the "x" range, then how about this:

VBA Code:
Sub autofil()

    Dim x As Range, sel As Range
    Dim numberOfMonths As Integer
    Dim strt As String, finsh As String
    numberOfMonths = 15
    Set x = Cells(23, numberOfMonths)
    Set sel = Selection
    strt = sel.Address(False, False)
    finsh = x.Address(False, False)
    Selection.AutoFill Destination:=Range(strt & ":" & finsh), Type:=xlFillDefault
    
End Sub
 
Upvote 0
Welcome to the MrExcel board!


Where?


Where?
Hello! Both inputs from the user are taken from cells within the worksheet. So they give me the starting month (January for example), and how many months out they want to go out to.
 
Upvote 0
I can understand why you would want to answer @Peter_SSs questions with his impressive credentials. I don't understand why you would not acknowledge my proposed solution, whether it worked or not.
 
Upvote 0
Thanks for the help igold, I appreciate it. I am still getting the same error though after I made some adjustments. This is what I did:
VBA Code:
Dim x As Range, sel As Range
Dim strt As String, finsh As String
Set x = Cells(23, numberOfMonths)
Set sel = Worksheets("Program Forecasting Details").Range("C23")
strt = sel.Address(False, False)
finsh = x.Address(False, False)
Selection.AutoFill Destination:=Range(strt & ":" & finsh), Type:=xlFillDefault

I changed "sel" to the range at which I would like the autofill to start. After running the macro, I got the same error of "Autofill Method of Range class Failed." Do you see where I went wrong? Thanks for the help again.
Hi ExcelDoer, welcome to the MrExcel Forum. If the begging of your autofil range is your "Selection" and the end of your range the "x" range, then how about this:

VBA Code:
Sub autofil()

    Dim x As Range, sel As Range
    Dim numberOfMonths As Integer
    Dim strt As String, finsh As String
    numberOfMonths = 15
    Set x = Cells(23, numberOfMonths)
    Set sel = Selection
    strt = sel.Address(False, False)
    finsh = x.Address(False, False)
    Selection.AutoFill Destination:=Range(strt & ":" & finsh), Type:=xlFillDefault
  
End Sub
 
Upvote 0
When Peter had asked "Where" and "Where", he was really asking for which cells specifically on the worksheet. Without knowing the actual cell addresses any solution such as mine is just throwing darts. If you could provide the actual cell addresses, such as where the autofil range will start and where it will end, then a solution should be pretty easy. I understand the user supplies some info but you have enter that info in a certain cells on the sheet. That is the key piece of information that is lacking.
 
Upvote 0
When Peter had asked "Where" and "Where", he was really asking for which cells specifically on the worksheet. Without knowing the actual cell addresses any solution such as mine is just throwing darts. If you could provide the actual cell addresses, such as where the autofil range will start and where it will end, then a solution should be pretty easy. I understand the user supplies some info but you have enter that info in a certain cells on the sheet. That is the key piece of information that is lacking.
The user enters the starting month in cell "B8", and the number of months they want to go out to in "B9".

Then, later on in my macro, I copy and paste the month that the user wants to start at to cell "C23" (supplied from the month given in "B8"). It is from "C23" where I want to autofill months out to the number of months they supplied from "B9". Does that make sense?
 
Upvote 0
So you are starting your autofil range @ cell "C23", are you then going down Column C to the number months (I assume one cell per month) or are you going across row 23 for the ending cell in your range.
 
Upvote 0
So you are starting your autofil range @ cell "C23", are you then going down Column C to the number months (I assume one cell per month) or are you going across row 23 for the ending cell in your range.
Yep, starting the autofill range at "C23", then I want to go to the right of C23 (D23, F23, E23, etc.) for one month per cell until I hit the number of months the user wanted the autofill function to go out to.

I feel like the solution you provided before should've worked but for some reason the autofill function isn't recognizing it. Maybe because "x" is used by the Cells() function and not Range()?
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

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