Userform Listbox

aellington287

New Member
Joined
Mar 7, 2016
Messages
7
I've created a userform with a listbox populated with each month. If somebody doesn't click a month before clicking "Okay", I want a message box telling them to choose a month before the program continues to run. I know how to create a message box, but what kind of if statement could I use to denote no object being chosen? Hopefully this makes sense. Thanks in advance.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi aellington287,

You can something like this:

Code:
Private Sub cmdOK_Click()

    If Me.ListBox1.ListIndex = -1 Then
        MsgBox "No selection has been made!!"
        Exit Sub
    Else
        'Code here for the selection made from the ListBox
    End If

End Sub

Another way is to set the Enabled property of your OK button to False when the form opens (so it's greyed out) then to True once a selection form the ListBox is made.

HTH

Robert
 
Upvote 0
Try something like this:
Modify to your liking
Code:
Private Sub CommandButton1_Click()
If ListBox1.Text = xlnull Then
MsgBox "You must select a month from the list"
Exit Sub
End If
Cells(1, 3).Value = ListBox1.Value
ListBox1.ListIndex = -1
End Sub
 
Last edited:
Upvote 0
You could disable the Okay button until they select a month

Code:
Private Sub Userform_Intialize()
     CommandButton1.Enabled = False
End Sub


Private Sub ListBox1_Change()
    CommandButton1.Enabled = (ListBox1.ListIndex <> -1)
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,066
Messages
6,128,586
Members
449,461
Latest member
jaxstraww1

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