• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4110
  • Last Modified:

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
0
andy7789
Asked:
andy7789
  • 4
  • 4
1 Solution
 
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
ioanePlanning & Analytics ManagerCommented:
Of course!!

Try:
If listbox.Selected(listbox.ListIndex) = True Then
0
 
andy7789Author 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
 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now