Consulting

Results 1 to 4 of 4

Thread: Array loop

  1. #1

    Array loop

    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
    Last edited by voyagerphoen; 12-10-2018 at 01:30 AM.

  2. #2
    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.

  3. #3
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    Hi voyagerphoen, You can also use UCase() or LCase() to realize it.

  4. #4
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •