Link to home
Start Free TrialLog in
Avatar of andy7789
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
Avatar of ioane
ioane
Flag of New Zealand image

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
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

Avatar of andy7789
andy7789

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....
Of course!!

Try:
If listbox.Selected(listbox.ListIndex) = True Then
ASKER CERTIFIED SOLUTION
Avatar of andy7789
andy7789

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.ListIndex) = False Then
yes, i did - see my previous comment. Your code cannot distinguish between "nothing selected" and "first item selected"