ComboBox ActiveX: Running Simple Code

Wamhoi

New Member
Joined
Mar 4, 2011
Messages
48
I created a ComboBox ActiveX directly on Sheet7 and a command button to execute an action based on variable chosen. I already defined the variables based on range in the outskirts of the sheet. I created the following vba code situated in the command button but have no luck.

Private Sub CommandButton10_Click()
Application.ScreenUpdating = False

Dim lItem As Long
For lItem = 0 To sheets7.ComboBox1.ListCount

If sheets7.ComboBox1.Selected(lItem) = True Then MsgBox "Hi"

Next
End Sub


Any Ideas?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
1)
I'm not seeing that "Selected" as a property of comboboxes. That is a property of listboxes. So you may be using a property that doesn't exist.

2)
sheets7 looks odd. might be Sheet7. You should see intellisense kicking in with your objects if they are correctly referenced.

3)
No need to turn screen updating off
 
Upvote 0
I updated my code but still no success

Private Sub CommandButton10_Click()
Dim lItem As Long
Dim Combo As MSForms.ComboBox
Set Combo = ActiveWorkbook.Sheets("Individual Summary").ComboBox1
For lItem = 0 To ComboBox1.ListCount
If Combo.Selected(lItem) = True Then MsgBox "Hi"
Exit For
Next
End Sub
 
Upvote 0
Where did you put the code and why are you looping through the combobox?

Can't you just find out which item has been selected by using Combo.Value or similar?

PS Why the Click event? Have you tried the default event Change?
 
Upvote 0
I'm not sure comboboxes have a selected property. Use the Value property. You can find out if anything is selected by checking the value when you first open the sheet - that's the value if the user chooses nothing. Probably Null or an empty string. Or perhaps you can check if there is a selection this way:

If Len(ComboBox1.Value & "") = 0 Then
Msgbox "Nothing Selected"
Else
Msgbox ComboBox1.Value
End If
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,299
Members
452,904
Latest member
CodeMasterX

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