ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Multiselect listbox if no selection (https://www.excelbanter.com/excel-programming/411292-multiselect-listbox-if-no-selection.html)

BigPig

Multiselect listbox if no selection
 
Hi All,

I have a listbox that is multiselect. I want to find a way to figure out if
anything is selected, if not "do something". I wrote this which works, but I
have to think that there is an easier way.

Dim Z As Integer
Z = 0
With lst_Comp_Lv
For i = 0 To .ListCount - 1
If .Selected(i) Then
Else
Z = Z + 1
End If
Next i
End With

If Z = lst_Comp_Lv.ListCount Then
MsgBox "There was nothing selected."
Exit Sub
End If

I also tried:

If lst_Comp_Lv = Null Then
MsgBox "There is nothing selected."
Exit Sub
End If

But that didn't work.

Any suggestions or advice you could provide would be greatly appreciated.
Thank you.


Jim Rech

Multiselect listbox if no selection
 
You do have to go through the list box an item at a time. You could quit
the loop as so as you find a selected item though.

Dim SomethingSelected as Boolean
With lst_Comp_Lv
For i = 0 To .ListCount - 1
If .Selected(i) Then
SomethingSelected = True
Exit For
End If
Next i
End With
If Not SomethingSelected Then
MsgBox "There was nothing selected."
End If


--
Jim
"BigPig" wrote in message
...
| Hi All,
|
| I have a listbox that is multiselect. I want to find a way to figure out
if
| anything is selected, if not "do something". I wrote this which works, but
I
| have to think that there is an easier way.
|
| Dim Z As Integer
| Z = 0
| With lst_Comp_Lv
| For i = 0 To .ListCount - 1
| If .Selected(i) Then
| Else
| Z = Z + 1
| End If
| Next i
| End With
|
| If Z = lst_Comp_Lv.ListCount Then
| MsgBox "There was nothing selected."
| Exit Sub
| End If
|
| I also tried:
|
| If lst_Comp_Lv = Null Then
| MsgBox "There is nothing selected."
| Exit Sub
| End If
|
| But that didn't work.
|
| Any suggestions or advice you could provide would be greatly appreciated.
| Thank you.
|



BigPig

Multiselect listbox if no selection
 
Hi Jim,

Thank you that worked.

Question:

Why wouldn't this work?

If lst_Comp_Lv = Null Then
MsgBox "There is nothing selected."
Exit Sub
End If

Thanks again.

Jim Rech

Multiselect listbox if no selection
 
If lst_Comp_Lv = Null Then

You didn't specified a property. A list box has several. What property of
the list box are you comparing to Null? The default propery is Value. So
what you're really asking is:

If lst_Comp_Lv.Value = Null Then

But what does the Value property of a list box represent? You've assumed it
represents whether any items are selected I guess but maybe it represents
the number of items in the listbox. Or its length or name or... So we
check Help.

Help says the Value proerty of a list box is "The value in the BoundColumn
of the currently selected rows."

That certainly has nothing to do with whether any items are selected. So
that's why it didn't help you.

--
Jim
"BigPig" wrote in message
...
| Hi Jim,
|
| Thank you that worked.
|
| Question:
|
| Why wouldn't this work?
|
| If lst_Comp_Lv = Null Then
| MsgBox "There is nothing selected."
| Exit Sub
| End If
|
| Thanks again.



BigPig

Multiselect listbox if no selection
 
Jim,

Yes I was assuming that if I didn't select anything, the value of the
listbox would be null.

Thank you for your thorough explanation.


All times are GMT +1. The time now is 06:52 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com