combobox click event

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
334
Office Version
  1. 2016
Platform
  1. Windows
I have a sheet where the user selects a particular month, through a combobox.

When he selects "January", then a Msgbox ("cold") should appear. And so on.

For some reason, the below code does not work:

VBA Code:
Private Sub monthCombo_Click()
Dim montH As String

Select Case montH
Case January
MsgBox ("cold")
'and so on
Case July
MsgBox ("hot")
'and so on until December
End Select

End Sub

what is wrong here ?
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi,

Several things can happen ...
1) How do you populate your ComboBox ?
2) Are you using Text or Numbers ?
3) Have you tried to use monthCombo_Change() with "January" and "July" ?
 
Upvote 0
It should be Case "January" with quotes, and so on for the other months.
 
Upvote 0
Hi James

the ChangeEvent doesn't work either.

This is how I popiulate the combobox:


VBA Code:
Private Sub Worksheet_Activate()
With ActiveSheet.monthCombo
.Clear
.AddItem "January"
.AddItem "February"
.AddItem "March"
.AddItem "April"
.AddItem "May"
.AddItem "June"
.AddItem "July"
.AddItem "August"
.AddItem "September"
.AddItem "October"
.AddItem "November"
.AddItem "December"
End With

End Sub
 
Upvote 0
Hi again,
VBA Code:
Private Sub monthCombo_Change()
Dim montH As String
    Select Case montH
        Case "January"
        MsgBox "Cold"
        'and so on
        Case "July"
        MsgBox "Hot"
        'and so on until December
    End Select
End Sub
 
Upvote 0
Solution
Thanks James and all

adding quotes helped. But also, I added as below:

VBA Code:
Private Sub monthCombo_Click()
Dim montH As String

[COLOR=rgb(184, 49, 47)][B]montH = monthCombo.Value[/B][/COLOR]

    Select Case montH
        Case "January"
        MsgBox "Cold"
        'and so on
        Case "July"
        MsgBox "Hot"
        'and so on until December
    End Select

End Sub
 
Upvote 0
Why not just use the combo directly?, why the middle man of montH ?
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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