Solved

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

Posted on 2009-06-29
8
3,277 Views
Last Modified: 2013-11-26
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
Comment
Question by:andy7789
  • 4
  • 4
8 Comments
 
LVL 13

Expert Comment

by:ioane
ID: 24742132
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
 
LVL 13

Expert Comment

by:ioane
ID: 24742143
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
 

Author Comment

by:andy7789
ID: 24742171
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
 
LVL 13

Expert Comment

by:ioane
ID: 24742182
Of course!!

Try:
If listbox.Selected(listbox.ListIndex) = True Then
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Accepted Solution

by:
andy7789 earned 0 total points
ID: 24742218
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
 

Author Comment

by:andy7789
ID: 24742231
Sorry, your code returns false, if nothing is selected OR the first item is selected.

0
 
LVL 13

Expert Comment

by:ioane
ID: 24742326
Did you try it like this?

If listbox.Selected(listbox.ListIndex) = False Then
0
 

Author Comment

by:andy7789
ID: 24742385
yes, i did - see my previous comment. Your code cannot distinguish between "nothing selected" and "first item selected"
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now