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
 
Hi,

If I understand ....

Put this line of cod before End Sub

ComboBox1.Value = Format(Date, "mmm-yy")

......
......
Next
ComboBox1.Value = Format(Date, "mmm-yy")
End Sub
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi,

If I understand ....

Put this line of cod before End Sub

ComboBox1.Value = Format(Date, "mmm-yy")

......
......
Next
ComboBox1.Value = Format(Date, "mmm-yy")
End Sub
I think based on DAVE's code is not relating for that!
 
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
VBA Code:
Dim DisableEvents   As Boolean
Dim ThisMonth       As String

These variables MUST be at the very TOP of your userforms code page OUTSIDE any procedure

Dave
 
Upvote 0
These variables MUST be at the very TOP of your userforms code page OUTSIDE any procedure


I copy the whole code as in post#9 , so the declaration of variables will be top of form !
 
Upvote 0
I copy the whole code as in post#9 , so the declaration of variables will be top of form !
then code should do what you want - works ok for me

If still have issues post back all the code in your form you are currently using.

Dave
 
Upvote 0
If still have issues post back all the code in your form you are currently using.


VBA 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
Hi
If this is the only codes you have in your userform then it should do what you asked for - I just copied it in to a userform with combobox & it works for me so if still an issue for you, I have no answer.

Dave
 
Upvote 0
Just question : when you run the form what show you ?
I suppose this shouldn't show when running the form
VBA Code:
MsgBox "sorry, the current month Is wrong", 48, "Invalid Selection"
 
Upvote 0
Just question : when you run the form what show you ?

When form first shown, The current month / year is displayed in combobox.
If I select another month / year the msgbox is diplayed & when dismissed, the current month / year is selected.

1734025764255.png
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,754
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