combobox click event

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
327
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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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
Shouldn't you be Selecting the combo control to test?
 
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,215,272
Messages
6,123,981
Members
449,138
Latest member
abdahsankhan

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