?
Solved

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

Posted on 2009-06-29
8
Medium Priority
?
3,761 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:ioane
ID: 24742182
Of course!!

Try:
If listbox.Selected(listbox.ListIndex) = True Then
0
 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
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…

719 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