Solved

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

Posted on 2009-06-29
8
3,624 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

636 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