Consulting

Results 1 to 5 of 5

Thread: Listbox multi select issue

  1. #1
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location

    Listbox multi select issue

    Can anyone tell me how why the below doesnt work?

    I want to display a message if my user selects more than one item from a userform listbox, I assume its because i ve put it into a loop and need to count items first? but not sure how to


    For i = 0 To ListBox1.ListCount - 1 If i <> 1 then msgbox " Please select one item only" Exit sub End if If ListBox1.Selected(i) Then MsgBox ListBox1.List(i, 0) & " is selected" End If Next

  2. #2
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    268
    Location
    .ListCount gives the number of items in the Listbox regardless of whether they are selected.

    Before going further, wouldn't it be easier just to set ListBox1.MultiSelect = False so that only one item can be selected at any time?

    (Then ListBox1.ListIndex will give you the index of the selected item from 0 to Listbox1.ListCount -1. I *think* that .ListIndex is set to -1 if nothing is selected.)

  3. #3
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    i cant do that unfortunately as i need my users to be able to multi select for another use from the same data in the list box

  4. #4
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    268
    Location
    Fair enough, then try something like:
    total = 0
    For i = 0 to ListBox1.ListCount - 1
    If ListBox1.Selected(i) Then total = total + 1
    Next i
    If total = 0 then
    MsgBox "Nothing Selected"
    ElseIf total > 1 then
    MsgBox "More than 1 Selected"
    End If

  5. #5
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    That worked as required

    Thanks Gibbo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •