UserForm1.ListBox1.Selected

poiu

Active Member
Joined
Sep 13, 2011
Messages
384
Hello,

Please can you tell me how to find if an item within my For Each loop below is selected? I'm aware of the UserForm1.ListBox1.Selected function but am not sure how to apply it to the responsibility_area variable.

Code:
For Each responsibility_area In UserForm1.ListBox1.List
        
    [B]' code to check if list item is selected[/B]
 
Next responsibility_area

Thanks,

Poiu
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
To check if a ListBox item is checked, one loops on the index.
Code:
Dim i As Long

With ListBox1
    For i = 0 to .ListCount - 1
        If .Selected(i) Then
            MsgBox .List(i) & " is selected."
        End If
    Next i
End With
If the ListBox is not multi-select, the .ListIndex property can be used

Code:
With ListBox1
    If .ListIndex = -1 Then
        MsgBox "nothing selected"
    Else
        MsgBox .List(.ListIndex) & " is selected."
    End If
End With
If you have a particular item that you are looking for.

Code:
With ListBox1
    For i = 0 to .ListCount - 1
        If .Selected(i) And .List(i) = "What I'm looking for" Then
            MsgBox .List(i) & " is selected."
        End If
    Next i
End With
or, in the non-multi-select situation.
Code:
With ListBox1
    If .Value = "What I'm looking for" Then MsgBox "It's selected!"
End With
 
Last edited:
Upvote 0
Code:
Dim n as long
with UserForm1.ListBox1
  For n = 1 to .ListCount
    If .Selected(n - 1) then
        ' do something
        msgbox .List(n - 1)
    End If
  Next n
End With
 
Upvote 0
you need to look at it this way

Code:
with UserForm1.ListBox1
  for i = 0 to .listcount -1 
    if .selected(i) then
      'code here
    end if
  next i
end with
 
Upvote 0

Forum statistics

Threads
1,224,502
Messages
6,179,126
Members
452,890
Latest member
Nikhil Ramesh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top