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
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Following your approach it could be:

VBA Code:
Dim MonthName As Variant

Private Sub ComboBox1_Change()
Dim thismonth As String
thismonth = MonthName(Month(Date)) & "-" & Right(Year(Date), 2)
If ComboBox1.Value <> thismonth Then
  MsgBox "sorry, the current month is different than system date"
  ComboBox1.Value = thismonth
End If
End Sub

Private Sub UserForm_Activate()
 
    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



Or rewriting it based on international locale code:

VBA Code:
Private Sub ComboBox1_Change()
Dim thismonth As String
thismonth = WorksheetFunction.Text(Date, "[$-409]mmm-yy")
If ComboBox1.Value <> thismonth Then
  MsgBox "sorry, the current month is different than system date"
  ComboBox1.Value = thismonth
End If
End Sub

Private Sub UserForm_Activate()
Dim i As Integer
    ' add 12 English months from position in the array
    For i = 1 To 12
        Me.ComboBox1.AddItem WorksheetFunction.Text(DateSerial(Year(Date), i, 1), "[$-409]mmm-yy")
    Next i
End Sub
 
Upvote 0
both two suggestions works but not completely !
I would also show current month automatically when run the form .
thanks
 
Upvote 0
But it is already shown in Change procedure. Use the same apprach.
So for instance:

VBA Code:
Private Sub ComboBox1_Change()
Dim thismonth As String
thismonth = WorksheetFunction.Text(Date, "[$-409]mmm-yy")
If ComboBox1.Value <> thismonth Then
  MsgBox "sorry, the current month is different than system date"
  ComboBox1.Value = thismonth
End If
End Sub

Private Sub UserForm_Activate()
Dim i As Integer
    ' add 12 English months from position in the array
    For i = 1 To 12
        Me.ComboBox1.AddItem WorksheetFunction.Text(DateSerial(Year(Date), i, 1), "[$-409]mmm-yy")
    Next i
ComboBox1.Value = WorksheetFunction.Text(Date, "[$-409]mmm-yy")
End Sub

But in this situation - why use combobox at all? May be this date shall be just label if a user cannot change it.
Or may be Change event shall just giva a warning, but leave the value as it has been selected by user. so:

VBA Code:
Private Sub ComboBox1_Change()
If ComboBox1.Value <> WorksheetFunction.Text(Date, "[$-409]mmm-yy") Then MsgBox "Warning: The current month is different than system date", vbExclamation
End Sub

Private Sub UserForm_Activate()
Dim i As Integer
    ' add 12 English months from position in the array
    For i = 1 To 12
        Me.ComboBox1.AddItem WorksheetFunction.Text(DateSerial(Year(Date), i, 1), "[$-409]mmm-yy")
    Next i
ComboBox1.Value = WorksheetFunction.Text(Date, "[$-409]mmm-yy")
End Sub
 
Upvote 0
should return for current month.
MonthName function exists in the application & you should not need to create an array of months.

See if following does what you want.

VBA Code:
Dim DisableEvents   As Boolean
Dim ThisMonth       As String
Private Sub ComboBox1_Change()
    If DisableEvents Then Exit Sub
    With Me.ComboBox1
        If .Value <> ThisMonth Then
            DisableEvents = True
            MsgBox "sorry, the current month Is wrong", 48, "Invalid Selection"
            .Value = ThisMonth
        End If
    End With
    DisableEvents = False
End Sub

Private Sub UserForm_Activate()
    Dim Yr          As Long, i As Long
   
    ' get last 2 digits of current year
    Yr = Right(Year(Date), 2)
   
    ThisMonth = Format(Date, "mmm-yy")
   
    ' add 12 months from monthname function
    With Me.ComboBox1
        .Clear
        For i = 1 To 12
            ' append current year to month
            .AddItem MonthName(i, True) & "-" & Yr
        Next i
        .Value = ThisMonth
        .Style = fmStyleDropDownList
    End With
   
End Sub

Dave
 
Upvote 0
Thanks Dave,
but unfortunately your code deal with language based on setting regional in my PC ,in this case will show Arabic!
based on my OP should show English months even setting regional in my PC is Arabic.
 
Upvote 0
Thanks Dave,
but unfortunately your code deal with language based on setting regional in my PC ,in this case will show Arabic!
based on my OP should show English months even setting regional in my PC is Arabic.

OK my misunderstanding, will need to re-instate your array

Make changes shown in BOLD & see if helps

Rich (BB code):
Option Base 1
Dim DisableEvents   As Boolean
Dim ThisMonth       As String
Private Sub ComboBox1_Change()
    If DisableEvents Then Exit Sub
    With Me.ComboBox1
        If .Value <> ThisMonth Then
            DisableEvents = True
            MsgBox "sorry, the current month Is wrong", 48, "Invalid Selection"
            .Value = ThisMonth
        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)
    
    ThisMonth = 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 = ThisMonth
        .Style = fmStyleDropDownList
    End With
    
End Sub
 
Upvote 0
when run the form will show message about no month selected , should just select current month when run the form
after message is gone will show error could not set the value property in this line
VBA Code:
.Value = ThisMonth
 
Upvote 0

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