andy7789
asked on
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
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
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
ASKER
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....
I need to find something to check it instantly. probably, i should check what the listbox.value is if nothing is selected....
Of course!!
Try:
If listbox.Selected(listbox.L istIndex) = True Then
Try:
If listbox.Selected(listbox.L
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry, your code returns false, if nothing is selected OR the first item is selected.
Did you try it like this?
If listbox.Selected(listbox.L istIndex) = False Then
If listbox.Selected(listbox.L
ASKER
yes, i did - see my previous comment. Your code cannot distinguish between "nothing selected" and "first item selected"
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