VBA - if combobox is blank

mcooer0913

New Member
Joined
Feb 16, 2009
Messages
22
I am trying to display a message reminding the user (using a userform) to select an option in a combobox. If the user hasn't selected an option, I would like to display a message saying something like "Please select an option." I have used the following code successfully with text boxes:

Private Sub cmdOK_Click()
If Me.txtFirstName.Value = "" Then
MsgBox "Please enter a First Name.", vbExclamation, "Staff Expenses"
Me.txtFirstName.SetFocus
Exit Sub
End If

But when I try to replace the txtFirstName with the combobox control, it doesn't work. Any suggestions?
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
What do you mean by "doesn't work"? You could test its ListIndex property - if it's -1 then nothing has been selected.
 
Upvote 0
I can't replicate that behaviour. Did you try checking the listindex?
 
Upvote 0
I can't replicate that behaviour. Did you try checking the listindex?

Like this?:

If cboStatus.ListIndex = "-1" Then
MsgBox "Please enter status (Active/Inactive) for this client.", vbExclamation, "Client Info"
Me.cboStatus.SetFocus
Exit Sub
End If
 
Upvote 0

Forum statistics

Threads
1,203,242
Messages
6,054,353
Members
444,718
Latest member
r0nster

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