How to tell if nothing is selected in multiselect enabled listbox

bradyboyy88

Well-known Member
Joined
Feb 25, 2015
Messages
562
I have a listbox that may or may not be populated with items. If it is populated then the users can select multiple items in the listbox. How do I go about checking if nothing has been selected in the listbox when its empty or has items in there? Can this be done without looping through every item in the list to check if it has been selected?

Thanks
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Where is the ListBox located at... on a UserForm or on a worksheet? If on a worksheet, what kind of ListBox is it... a Forms control or an ActiveX control?
 
Upvote 0
Where is the ListBox located at... on a UserForm or on a worksheet? If on a worksheet, what kind of ListBox is it... a Forms control or an ActiveX control?

It is located on a userform named userform1 inside a frame called frame1. SOrry I should have been more specific!
 
Upvote 0
Assuming your listbox is named ListBox1, put this in whatever procedure you want to use to check the listbox from...
Code:
Dim X As Long, FoundOne As Boolean
For X = 0 To ListBox1.ListCount
  If ListBox1.Selected(X) Then
    FoundOne = True
    Exit For
  End If
Next
If FoundOne Then
  MsgBox "At least one item is selected"
Else
  MsgBox "No items are selected"
End If
 
Upvote 0
Its strange Visual basic has a method to check without looping through and vba doesnt but I guess this will do! Thanks for the code!
 
Upvote 0
Assuming your listbox is named ListBox1, put this in whatever procedure you want to use to check the listbox from...
Code:
Dim X As Long, FoundOne As Boolean
For X = 0 To ListBox1.ListCount
  If ListBox1.Selected(X) Then
    FoundOne = True
    Exit For
  End If
Next
If FoundOne Then
  MsgBox "At least one item is selected"
Else
  MsgBox "No items are selected"
End If

One question though. Why do we not have to subtract 1 for the listbox1.listcount? Say there was 2 items in the listbox then technically it should be such that listcount gives 3 so .selected(3) shouldnt work since that starts 0 to 2 I thought. Its not drawing an error and I cannot figure out why.
 
Upvote 0
One question though. Why do we not have to subtract 1 for the listbox1.listcount? Say there was 2 items in the listbox then technically it should be such that listcount gives 3 so .selected(3) shouldnt work since that starts 0 to 2 I thought. Its not drawing an error and I cannot figure out why.
Thank you for noting that... yes, it should be ListBox1 - 1 and for the reason you stated.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,530
Messages
6,160,351
Members
451,639
Latest member
Kramb

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