Create variable month list in a column.

mopey12345

Board Regular
Joined
Nov 26, 2020
Messages
76
Office Version
  1. 2010
Platform
  1. Windows
Hi everyone,
I have to create a list of months 6 (min) up to 12 (max) using any month as the start month. Two cells define the start month and the number of months. Is there a better way than creating 84 arrays to cover all instances? eg JANMONTHNAMES6 = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun""), upto JANMONTHNAMES12= Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), for each month. Thanks in advance.
 
When you first start it's often easy to overthink tasks and some of the help guides that you find are not always helpful.

See if this does the trick
VBA Code:
Sub test()
Range("D3").Value = Range("C3").Value
Range("D3").AutoFill Destination:=Range("D3").Resize(, Range("C4").Value), Type:=xlFillDefault
End Sub
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
When you first start it's often easy to overthink tasks and some of the help guides that you find are not always helpful.

See if this does the trick
VBA Code:
Sub test()
Range("D3").Value = Range("C3").Value
Range("D3").AutoFill Destination:=Range("D3").Resize(, Range("C4").Value), Type:=xlFillDefault
End Sub
Thanks so much for helping, exactly right about over complicating. Regards Phil
 
Upvote 0
Thanks so much for helping, exactly right about over complicating. Regards Phil
Hi
Sorry to ask again but hit another snag. Getting a run time error 1004 , application or object not defined on autofill line.
Sub test()
With Worksheets("Sheet2")
Range("D3").Value = Range("C3").Value

Range("D3").AutoFill Destination:=Range("D3").Resize(, Range("C4").Value), Type:=xlFillDefault
End With
End Sub
 
Upvote 0
Put a dot in front of each Range object to they point to the worksheet defined by the With statement... without the dot, those ranges default to the active worksheet.

Note: Since your example code showed you want the month names down Column D, you need to remove the comma inside the Resize property's argument (with the comma, the months would be listed across Row 3 instead).

Here is the code with the revisions I suggested...
VBA Code:
Sub test()
  With Worksheets("Sheet2")
    .Range("D3").Value = .Range("C3").Value
    .Range("D3").AutoFill Destination:=.Range("D3").Resize(.Range("C4").Value), Type:=xlFillDefault
  End With
End Sub

Just pointing out that the above code is more verbose than it needs to be. True, it is probably more understandable especially if you are new to VBA, but so you know, the following is equivalent to the modified code I posted above...
VBA Code:
Sub test()
  With Worksheets("Sheet2")
    .[D3] = .[C3]
    .[D3].AutoFill .[D3].Resize(.[C4])
  End With
End Sub
 
Last edited:
Upvote 0
I was just typing up a reply but Rick beat me to it so no point in duplicating the same reply.

Thanks for picking up on the resize error, Rick (y)
 
Upvote 0
I was just typing up a reply but Rick beat me to it so no point in duplicating the same reply.

Thanks for picking up on the resize error, Rick (y)
Put a dot in front of each Range object to they point to the worksheet defined by the With statement... without the dot, those ranges default to the active worksheet.

Note: Since your example code showed you want the month names down Column D, you need to remove the comma inside the Resize property's argument (with the comma, the months would be listed across Row 3 instead).

Here is the code with the revisions I suggested...
VBA Code:
Sub test()
  With Worksheets("Sheet2")
    .Range("D3").Value = .Range("C3").Value
    .Range("D3").AutoFill Destination:=.Range("D3").Resize(.Range("C4").Value), Type:=xlFillDefault
  End With
End Sub

Just pointing out that the above code is more verbose than it needs to be. True, it is probably more understandable especially if you are new to VBA, but so you know, the following is equivalent to the modified code I posted above...
VBA Code:
Sub test()
  With Worksheets("Sheet2")
    .[D3] = .[C3]
    .[D3].AutoFill .[D3].Resize(.[C4])
  End With
End Sub
Thank you for helping. Much appreciated as it would take me ages to figure out, if at all. Regards Phil
 
Upvote 0
I was just typing up a reply but Rick beat me to it so no point in duplicating the same reply.

Thanks for picking up on the resize error, Rick (y)
Thanks again for all your help. Much appreciated. regards Phil
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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