PDA

View Full Version : [SOLVED] Listbox multi select issue



gibbo1715
08-31-2005, 11:29 AM
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

BlueCactus
08-31-2005, 11:32 AM
.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.)

gibbo1715
08-31-2005, 11:35 AM
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

BlueCactus
08-31-2005, 11:39 AM
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

gibbo1715
08-31-2005, 01:23 PM
That worked as required

Thanks Gibbo