VBA Create Message of List Box item not selected

scotball

Active Member
Joined
Oct 23, 2008
Messages
375
I have a form with a list box and the following code which populates the list

Code:
With GroupListBox    .AddItem "HMP Low Moss"
    .AddItem "HMP Barlinnie"
    .AddItem "HMP Greenock"
    .AddItem "HMP Edinburgh"

This only populates the items, it doesn't select an item. I need a piece of code which prompts the user to make a selection before the form is submitted.

I have this code so far but it doesn't work:

Code:
If GroupListBox.Value = False ThenMsgBox "Please add in Location", vbExclamation
  Exit Sub
End If

Any advice welcomed,
Lee
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Is your listbox single or multi select?
 
Upvote 0
In that case try
Code:
If GroupListBox.Value = "" Then
   MsgBox "Please add in Location", vbExclamation
   Exit Sub
End If
 
Upvote 0
Code:
Private Sub OkButton_Click()       
Dim emptyRow As Long
emptyRow = Range("A" & Rows.Count).End(xlUp).Row + 1


If NameTextBox.Value = "" Then
MsgBox "Please add in Name", vbExclamation
  Exit Sub
End If


If GroupListBox.Value = "" Then
   MsgBox "Please add in Location", vbExclamation
   Exit Sub
End If


If SessionsBox.Value = "" Then
MsgBox "Please add in Session Date", vbExclamation
  Exit Sub
End If


If DOBTextBox.Value = "" Then
MsgBox "Please add in DOB", vbExclamation
  Exit Sub
End If


If CommentsTextBox.Value = "" Then
MsgBox "Please add in Comments", vbExclamation
  Exit Sub
End If

That didnt work - the above code is where I've placed it in the Sub
 
Upvote 0
If you want to check if something has been selected in a single select listbox check the ListIndex property, if it's -1 nothing has been selected.
Code:
If GroupListBox.ListIndex = -1 Then
   MsgBox "Please add in Location", vbExclamation
   Exit Sub
End If
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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