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
 
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.
Here's a picture of the situation I have within my worksheet if it helps:
1723648775158.png

I would just like the macro to start from the cell that has January, and go out 8 months through the autofill drag method.
 
Upvote 0

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.
If C23 is always your start point 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 + 3)
    Set sel = Range("C23")
    strt = sel.Address(False, False)
    finsh = x.Address(False, False)
    Range("C23").AutoFill Destination:=Range(strt & ":" & finsh), Type:=xlFillDefault
    
End Sub
 
Upvote 1
Solution
If C23 is always your start point 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 + 3)
    Set sel = Range("C23")
    strt = sel.Address(False, False)
    finsh = x.Address(False, False)
    Range("C23").AutoFill Destination:=Range(strt & ":" & finsh), Type:=xlFillDefault
   
End Sub
I still get the same error, "Autofill method of Range class failed." :( My only guess is because within the autofill function, the range we are providing isn't valid for some reason.
 
Upvote 0
I still get the same error, "Autofill method of Range class failed." :( My only guess is because within the autofill function, the range we are providing isn't valid for some reason.
Never mind I made a typo error, that solved it !!! Thank you so much for the help @igold - I was going crazy trying to figure this out. I really appreciate your help!
 
Upvote 0
You're welcome. We were all happy to help! Thanks for the feedback!
 
Upvote 0
If you already have "January" (or whatever month) in C23 based on the value in B8 and the number of months is in B9 then this single line should do what you want?

VBA Code:
Sub Test()
  Range("C23").AutoFill Destination:=Range("C23").Resize(, Range("B9").Value), Type:=xlFillDefault
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,107
Members
453,021
Latest member
Justyna P

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