automatically gets month in an array

G

Guest

hi...

I want to run a loop such that if the starting value is
oct then my array arrMonth() should start with OCtober or
OCt and the remaining 11 months be filled automatically...

is it feasible?

thanks a lot
 
B

Bob Kilmer

Do you also mean that if the first value is, say, feb, then your array would
start with FEb, and be filled in with the next 11 months in calendar order?
 
T

Tom Ogilvy

Sub Tester1AAA()
Dim mths(1 To 12) as String
j = 2
For i = 1 To 12
mths(i) = Format(DateSerial(Year(Date), j, 1), "mmmm")
j = j + 1
Next
For i = 1 To 12
Debug.Print mths(i)
Next
End Sub

Excel will "roll" the month back to January after you reach December and
give you the correct result regardless of the start month.
 
T

Tushar Mehta

Anything's possible :)

Function setMonths(FirstMonth As Byte) As String()
Dim MthNames(11) As String, i As Byte
For i = 0 To 11
MthNames(i) = Choose((FirstMonth + i - 1) Mod 12 + 1, _
"Jan", "Feb", "Mar", "Apr", "May", "Jun", _
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
Next i
setMonths = MthNames
End Function
Sub testIt2()
Dim MthNames() As String, FirstMonth As Byte
FirstMonth = 6
MthNames = setMonths(FirstMonth)
End Sub

The above needs VB6.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions
 
D

Dana DeLouis

Don't know how you want it set up, but would any ideas here help? Later
versions of Excel make this faster.

Sub Demo()
Dim Months(1 To 12) As String
Dim Start As Long
Dim Mth As Long

Start = 10 'October

For Mth = 1 To 12
Months(Mth) = MonthName(((Mth + Start - 2) Mod 12) + 1)
Next Mth
End Sub
 
Top