PDA

View Full Version : Solved: Check if List box has anything selected in it



malik641
12-17-2005, 12:12 PM
Is there an easy way to check if a list box has anything selected in it? And by easy I mean without loops.

I've tried this, but no luck...

If Not lstStudy.ListIndex Then ...

Ideas?

EDIT: I'm using the MultiSelect feature for the list box, BTW. And I realized that I can't use the ListIndex property for this. I've also tried the following since the realization:

If lstStudy.Selected (-1) Then...
If Not lstStudy.Selected Then...


Still no luck...hmmm :think:

And here's what I have now, BTW:
Dim i as Long
Dim lstSelected as Boolean
Dim NoItemsList as String

If chkStudy Then
For i = 0 To lstStudy.ListCount - 1
If lstSelected = True Then Exit For
If Not lstStudy.Selected(i) Then lstSelected = False Else lstSelected = True
Next i
If lstSelected = False Then NoItemsList = NoItemsList & chkStudy.Caption & vbCrLf
End If

mdmackillop
12-17-2005, 04:12 PM
How about

If IsNull(ListBox1) Then
MsgBox "Nothing Selected"
Else
MsgBox ListBox1
End If

malik641
12-17-2005, 04:57 PM
:think: Not working for me. My IF statement is supposed to check to see if a check box is checked and if there is nothing selected in the corresponding listbox. But the following code comes up no matter if I have anything selected in the list box or not.

If chkStudy And IsNull(lstStudy) Then NoItemsList = NoItemsList & chkStudy.Caption & vbCrLf

NoItemsList is just a collection of strings for the MsgBox later on in the code, BTW.

tpoynton
12-17-2005, 05:46 PM
this is how i do it for multiselect boxes; it involves a loop, but you do not appear too averse to using them...

For i = 0 To ListBox.ListCount - 1
If ListBox.Selected(i) = True Then
iCount = iCount + 1
End If
Next i


if iCount = 0 nothing was selected...hope this helps! it looks like you could easily incorporate this into your code above.

mdmackillop
12-17-2005, 06:10 PM
Try the attached

malik641
12-17-2005, 09:37 PM
Thanks tpoynton. That's pretty much what I'm using now (take a look at my first thread again). The difference is if an item is selected before the loop is over, it will exit the loop. Then the boolean lstSelected is true, and the if statement will not happen.

And what's with the avatar? It's interesting......Looks like "The summation of Za"...brings me back to Physics and Calculus :)



Malcom, thanks for the demo. But try the following and you'll see what I'm talking about:
1. Click button 1 on sheet 1.
2. Select check box to be "checked"
3. Select 1 and press commandbutton1. Message "Tick, List = 0" (no problem here)
3. (While userform is still up) Deselect 1. Select 2, press commandbutton1. Message "Tick, List selected".
4. Deselect 2. Press commandbutton1. Message "Tick, List selected" still comes up :think:

And that's what I'm talking about.....no clue why that happens either.

mdmackillop
12-18-2005, 03:15 AM
Hi Joseph,
I see in Help that ListIndex is not applicable to multiselect boxes. From the following version of my code, you can see that ListIndex is set as the last value clicked, whether on or off.

Private Sub UserForm_Initialize()
lstStudy.List = Range("Data").Value
lstStudy.ListIndex = -1
End Sub
Private Sub CommandButton1_Click()
MsgBox lstStudy
If Not (chkStudy) And lstStudy.ListIndex >= 0 Then
MsgBox "No Tick, List = " & lstStudy.ListIndex
End If
If chkStudy And lstStudy.ListIndex >= 0 Then
MsgBox "Tick, List = " & lstStudy.ListIndex
End If
If Not (chkStudy) And Not (lstStudy.ListIndex >= 0) Then
MsgBox "No Tick, List = " & lstStudy.ListIndex
End If
If chkStudy And Not (lstStudy.ListIndex >= 0) Then
MsgBox "Tick, list = " & lstStudy.ListIndex
End If
End Sub

tpoynton
12-18-2005, 08:45 AM
The other way around this issue is to have something selected in the listbox when loaded; Public Sub Userform_Activate()
ListBox.Selected(0) = True
End Sub will select the first item in the listbox upon activation. this forces the user's hand in a sense, but prevents you from having to write a bunch of code to deal with user errors...

malik641
12-19-2005, 08:14 AM
The other way around this issue is to have something selected in the listbox when loaded; Public Sub Userform_Activate()
ListBox.Selected(0) = True
End Sub will select the first item in the listbox upon activation. this forces the user's hand in a sense, but prevents you from having to write a bunch of code to deal with user errors...
Still won't do what I need it to...:think:

And I tried that with Malcom's userform with the following code:
Private Sub UserForm_Initialize()
lstStudy.List = Range("Data").Value
lstStudy.ListIndex = -1
End Sub
Private Sub UserForm_Activate()
lstStudy.Selected(0) = True
End Sub
Private Sub CommandButton1_Click()
'MsgBox lstStudy
If Not (chkStudy) And lstStudy.ListIndex >= 0 Then
MsgBox "No Tick, List = " & lstStudy.ListIndex
End If
If chkStudy And lstStudy.ListIndex >= 0 Then
MsgBox "Tick, List = " & lstStudy.ListIndex
End If
If Not (chkStudy) And Not (lstStudy.ListIndex >= 0) Then
MsgBox "No Tick, List = " & lstStudy.ListIndex
End If
If chkStudy And Not (lstStudy.ListIndex >= 0) Then
MsgBox "Tick, list = " & lstStudy.ListIndex
End If
End Sub


I guess I'm just going to have to stick with what I have:
'Sponsor
If chkSponsor Then
For i = 0 To lstSponsor.ListCount - 1
If Not lstSponsor.Selected(i) Then lstSelected = False Else lstSelected = True
If lstSelected = True Then Exit For
Next i
If lstSelected = False Then NoItemsList = NoItemsList & chkSponsor.Caption & vbCrLf
End If
'Study Number
If chkStudy Then
For i = 0 To lstStudy.ListCount - 1
If Not lstStudy.Selected(i) Then lstSelected = False Else lstSelected = True
If lstSelected = True Then Exit For
Next i
If lstSelected = False Then NoItemsList = NoItemsList & chkStudy.Caption & vbCrLf
End If
'Validation / Production
If chkValidation Then
For i = 0 To lstValidation.ListCount - 1
If Not lstValidation.Selected(i) Then lstSelected = False Else lstSelected = True
If lstSelected = True Then Exit For
Next i
If lstSelected = False Then NoItemsList = NoItemsList & chkValidation.Caption & vbCrLf
End If
'Project manager
If chkProject Then
For i = 0 To lstProject.ListCount - 1
If Not lstProject.Selected(i) Then lstSelected = False Else lstSelected = True
If lstSelected = True Then Exit For
Next i
If lstSelected = False Then NoItemsList = NoItemsList & chkProject.Caption & vbCrLf
End If
'Advanced Criteria (for date filtering)
If chkCriteria Then
For i = 0 To lstCriteria.ListCount - 1
If Not lstCriteria.Selected(i) Then lstSelected = False Else lstSelected = True
If lstSelected = True Then Exit For
Next i
If lstSelected = False Then NoItemsList = NoItemsList & chkCriteria.Caption & vbCrLf
End If
'--------------------------------------------------------------------
'If any of the above is true
If NoItemsList <> vbNullString Then
MsgBox NoItemsList & vbCrLf & msgNoItems
Exit Sub
End If

If I could've found one line for each check, I would have reduced my code tremendously...:mkay Oh well...

Thanks for all your help guys :thumb I appreciate it. If you happen to figure something out, please post it...otherwise I'm marking it solved for now...cause it's not that big of an issue for me and I really don't have the time to try to figure something out for this (working on a heavy deadline).

Ty0
12-01-2015, 10:33 PM
I guess I'm just going to have to stick with what I have:

'Sponsor
If chkSponsor Then
For i = 0 To lstSponsor.ListCount - 1
If Not lstSponsor.Selected(i) Then lstSelected = False Else lstSelected = True
If lstSelected = True Then Exit For
Next i
If lstSelected = False Then NoItemsList = NoItemsList & chkSponsor.Caption & vbCrLf
End If
'Study Number
If chkStudy Then
For i = 0 To lstStudy.ListCount - 1
If Not lstStudy.Selected(i) Then lstSelected = False Else lstSelected = True
If lstSelected = True Then Exit For
Next i
If lstSelected = False Then NoItemsList = NoItemsList & chkStudy.Caption & vbCrLf
End If
'Validation / Production
If chkValidation Then
For i = 0 To lstValidation.ListCount - 1
If Not lstValidation.Selected(i) Then lstSelected = False Else lstSelected = True
If lstSelected = True Then Exit For
Next i
If lstSelected = False Then NoItemsList = NoItemsList & chkValidation.Caption & vbCrLf
End If
'Project manager
If chkProject Then
For i = 0 To lstProject.ListCount - 1
If Not lstProject.Selected(i) Then lstSelected = False Else lstSelected = True
If lstSelected = True Then Exit For
Next i
If lstSelected = False Then NoItemsList = NoItemsList & chkProject.Caption & vbCrLf
End If
'Advanced Criteria (for date filtering)
If chkCriteria Then
For i = 0 To lstCriteria.ListCount - 1
If Not lstCriteria.Selected(i) Then lstSelected = False Else lstSelected = True
If lstSelected = True Then Exit For
Next i
If lstSelected = False Then NoItemsList = NoItemsList & chkCriteria.Caption & vbCrLf
End If
'--------------------------------------------------------------------
'If any of the above is true
If NoItemsList <> vbNullString Then
MsgBox NoItemsList & vbCrLf & msgNoItems
Exit Sub
End If


Consider:


Sub NoSelectionsMsg(Chk As CheckBox, LB As ListBox, Msg As String)
Dim l As Long
Dim ErrNo As Variant

If Chk Then
On Error Resume Next
l = LB.ItemsSelected(0) 'Will throw error if no items selected.
ErrNo = Err.Number
On Error GoTo 0
If ErrNo <> 0 Then Msg = Msg & Chk.Caption & vbCrLf
End If
End Sub

'Sponsor
NoSelectionsMsg chkSponsor, lstSponsor, NoItemsList
'Study Number
NoSelectionsMsg chkStudy, lstStudy, NoItemsList
'Validation / Production
NoSelectionsMsg chkValidation, lstValidation, NoItemsList
'Project manager
NoSelectionsMsg chkProject, lstProject, NoItemsList
'Advanced Criteria (for date filtering)
NoSelectionsMsg chkCriteria, lstCriteria, NoItemsList
'--------------------------------------------------------------------
'If any lists above have no selections
If NoItemsList <> vbNullString Then MsgBox NoItemsList & vbCrLf & msgNoItems

mancubus
12-02-2015, 12:18 AM
@Ty0

i'm just wondering your motivation to post to a 10 years old thread...
and this is your first message to the forum.