
Newbie q: Remove selected items from multiselect listbox?
Your approach would work if you use a ListView (more elegant) where you can use
the ListView's
ListItems collection. Otherwise, this example uses two Collections with a
ListBox.
Option Explicit
Dim Selected As New Collection
Dim Unselected As New Collection
==========================
Private Sub Command1_Click()
Dim i As Long
Dim t As Long
Dim j As Variant
t = List1.ListCount
If t = 0 Then Exit Sub
'Loop List1 and add each item to the appropriate Collection
For i = 0 To t - 1
If List1.Selected(i) Then
Selected.Add List1.List(i)
Else
Unselected.Add List1.List(i)
End If
Next
List1.Clear
'Repopulate List1 with the items in Unselected
For Each j In Unselected
List1.AddItem j
Next
'Likewise, you can do something with the selected items
'For Each j In Selected
'(Do some process)
'Next
Set Selected = Nothing
Set Unselected = Nothing
End Sub
Rob
Quote:
> What is the best solution for removing selected items (I used Multiselect=1
> <simple>) from a listbox?
> I *know* this doesn't work:
> For t = 1 To List1.ListCount()
> If List1.Selected(t - 1) Then
> List1.RemoveItem (t - 1)
> End If
> Next t
> because by the time the counter reaches List1.Listcount, some items are
> already deleted. But what is the "elegant" solution.
> Thanks in advance,
> Richard Douwes