PDA

View Full Version : Array loop



voyagerphoen
12-10-2018, 12:52 AM
Im using office 2007 in windows 7 pc.this code uses names of months from sheet1. when i run this code it should pop up message in msgbox as i found in tutorial.But instead this do nothing.

Sub currentmontharray()
Dim months(11) As String
Dim i As Integer
Dim xrow As Long
i = 0
xrow = 2




Do Until Cells(xrow, 1).Value = ""
months(i) = Cells(xrow, 1).Value
i = i + 1
xrow = xrow + 1
Loop

For i = 0 To UBound(months, 1)
If months(i) = MonthName(Month(Date)) Then
MsgBox ("current month is " & MonthName(Month(Date)))
End If
Next i
End Sub

voyagerphoen
12-10-2018, 01:32 AM
I found the way to correct code i.e. I inserted months name in small letters instead of first capital. I think vba differentiates small caps and first caps.

大灰狼1976
12-10-2018, 10:56 PM
Hi voyagerphoen, You can also use UCase() or LCase() to realize it.

jolivanes
12-10-2018, 11:39 PM
If you are using the regular 12 months of the year from sheet1, you can use this instead if you want.

Sub Get_Months_Short()
Dim a
ReDim a(1 To 11)
a = Application.GetCustomListContents(3)
'Range("C1").Resize(UBound(a)) = Application.Transpose(a)
End Sub



Sub Get_Months_Long()
Dim a
ReDim a(1 To 11)
a = Application.GetCustomListContents(4)
'Range("C1").Resize(UBound(a)) = Application.Transpose(a)
End Sub


The data can be found in the "Edit Custom List" under "General" in Excel Options - Advanced.
The line numbers where the data is is the number (here 3 and 4) you use.