Select current month in combobox on userform for English language

Ali M

Active Member
Joined
Oct 10, 2021
Messages
347
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hi,
I would when run the form then will select DEC-24 automatically , if I try select month is not current month then will show message "sorry, the current month is wrong" and should select DEC-24 again.
I would deal with English months as inside the code , because the language is Arabic in my PC .
VBA Code:
Option Base 1

Private Sub ComboBox1_Change()
If ComboBox1.Value <> Format(ComboBox1, "mmm-yy") Then MsgBox "sorry, the current month is wrong": Exit Sub
End Sub

Private Sub UserForm_Activate()
 Dim MonthName As Variant

    Dim Yr As Long

    MonthName = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")

    ' get last 2 digits of current year

    Yr = Right(Year(Date), 2)

    Dim i As Integer

    ' add 12 English months from position in the array

    For i = 1 To 12

        ' append current year to month

        Me.ComboBox1.AddItem MonthName(i) & "-" & Yr

    Next

End Sub
thanks
 
ok
so I guess the problem from EXCEL version !.
currently use 2010,2016

I no longer have access to either of those Excel versions but would not have thought, there is anything in the structure of the code that would be version dependent.
If can place copy of your workbook on file sharing site like dropbox will have further look.

Dave
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi
worked perfectly ok for me.

Just a guess but try this updated version & see what happens

Rich (BB code):
Option Base 1
Dim DisableEvents   As Boolean
Dim WhichMonth       As String
Private Sub ComboBox1_Change()
    If DisableEvents Then Exit Sub
    With Me.ComboBox1
        If .Value <> WhichMonth Then
            DisableEvents = True
            MsgBox "sorry, the current month Is wrong", 48, "Invalid Selection"
            .Value = WhichMonth
        End If
    End With
    DisableEvents = False
End Sub

Private Sub UserForm_Activate()
    Dim Yr          As Long, i As Long
    Dim MonthNames  As Variant
    
    ' get last 2 digits of current year
    Yr = Right(Year(Date), 2)
    
    WhichMonth = Format(Date, "mmm-yy")
    
    MonthNames = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
    
    ' add 12 months from monthname function
    With Me.ComboBox1
        .Clear
        For i = 1 To 12
            ' append current year to month
            .AddItem MonthNames(i) & "-" & Yr
        Next i
        .Value = WhichMonth
        .Style = fmStyleDropDownList
    End With
    
End Sub

Dave



1734079321216.png
 
Upvote 0
It's probably a regional issue. Try changing this line:

Code:
    WhichMonth = Format(Date, "mmm-yy")

to this:

Code:
    WhichMonth = MonthNames(Month(Date)-1) & "-" & Yr
 
Upvote 0
I don't see how that is possible - please post the code as you used it. I did just note that you are using Option Base 1 for some reason, so it should just be:

Code:
WhichMonth = MonthNames(Month(Date)) & "-" & Yr
 
Upvote 0
here is the whole code
VBA Code:
Option Base 1
Dim DisableEvents   As Boolean
Dim WhichMonth       As String
Private Sub ComboBox1_Change()
    If DisableEvents Then Exit Sub
    With Me.ComboBox1
        If .Value <> WhichMonth Then
            DisableEvents = True
            MsgBox "sorry, the current month Is wrong", 48, "Invalid Selection"
            .Value = WhichMonth
        End If
    End With
    DisableEvents = False
End Sub

Private Sub UserForm_Activate()
    Dim Yr          As Long, i As Long
    Dim MonthNames  As Variant
    
    ' get last 2 digits of current year
    Yr = Right(Year(Date), 2)
    
    'WhichMonth = Format(Date, "mmm-yy")
        'WhichMonth = MonthNames(Month(Date) - 1) & "-" & Yr
        WhichMonth = MonthNames(Month(Date)) & "-" & Yr


    
    MonthNames = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
    
    ' add 12 months from monthname function
    With Me.ComboBox1
        .Clear
        For i = 1 To 12
            ' append current year to month
            .AddItem MonthNames(i) & "-" & Yr
        Next i
        .Value = WhichMonth
        .Style = fmStyleDropDownList
    End With
    
End Sub
 
Upvote 0
Ah - move this line above the one I posted:

Code:
MonthNames = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
 
Upvote 0
Solution

Forum statistics

Threads
1,224,590
Messages
6,179,753
Members
452,940
Latest member
rootytrip

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