Solved

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

Posted on 2009-06-29
8
3,204 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
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…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

746 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

11 Experts available now in Live!

Get 1:1 Help Now