VBA excel: how to check if a list box (multiselect) is not selected

Hi x-perts,

how can I check if  none of the listbox items for a multiselect box are selected?

For a single select box I can use

listBox.ListIndex <> -1

But for a multiSelect box it has a value 0, if nothing is selected as well as the first item is selected

Thanks
andy7789Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
andy7789Connect With a Mentor Author Commented:
here is the working code. I have not tested yours yet
Public Function testMultiSelect(ByRef lst As MSForms.ListBox) As Boolean
If (lst.Selected(0) = False And lst.ListIndex = 0) Then
    testMultiSelect = False
Else
    testMultiSelect = True
End If
End Function

Open in new window

0
 
ioanePlanning & Analytics ManagerCommented:
Try this:

Private Function IsRowSelected() as Boolean
Dim i as Integer

IsRowSelected = False
For i = 0 to listbox.ListCount-1
  If listbox.Selected(i) = True Then IsRowSelected = True
  Exit For
Next i
End Function
0
 
ioanePlanning & Analytics ManagerCommented:
Sorry, should be this:


Private Function IsRowSelected() as Boolean
Dim i as Integer
 
IsRowSelected = False
For i = 0 to listbox.ListCount-1
  If listbox.Selected(i) = True Then 
    IsRowSelected = True
    Exit For
  End If
Next i
End Function

Open in new window

0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
andy7789Author Commented:
Well, it could work, but I cannot afford looping here just to see that nothing is selected, because that list may be huge.

I need to find something to check it instantly. probably, i should check what the listbox.value is if nothing is selected....
0
 
ioanePlanning & Analytics ManagerCommented:
Of course!!

Try:
If listbox.Selected(listbox.ListIndex) = True Then
0
 
andy7789Author Commented:
Sorry, your code returns false, if nothing is selected OR the first item is selected.

0
 
ioanePlanning & Analytics ManagerCommented:
Did you try it like this?

If listbox.Selected(listbox.ListIndex) = False Then
0
 
andy7789Author Commented:
yes, i did - see my previous comment. Your code cannot distinguish between "nothing selected" and "first item selected"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.