RE: delete multiple selected items from listbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

u try these:

first i think u already set the ListBox.RowSourceType = "Value List"

For Each IdxSource In LstSource.ItemsSelected
LstSource.RemoveItem (IdxSource) <-------- change to:
LstSource.RemoveItem Index:=IdxSource
Next IdxSource



"zigi" 來函:
 
thank you rainbow again,

but too bat, it didn't work.

If I select two or more items in the listbox, it deletes the first selected
item and then it exits the for-loop.

any other suggestions?
 
Hello,

I found the following solution on the web after days of searching:
The way is to put all NOT-selected items in an array, delete the whole
listbox and then fill the listbox aggain with all items from the array.


Dim ArrSize As Integer
Dim i As Integer
Dim TempFilesArray()

ArrSize = 0
For i = 0 To LstBox.ListCount - 1
If LstBox.Selected(i) = False Then
ArrSize = ArrSize + 1
ReDim Preserve TempFilesArray(ArrSize)
TempFilesArray(ArrSize) = LstBox.ItemData(i)
End If
Next

For i = 0 To LstBox.ListCount - 1
LstBox.RemoveItem (0)
Next i

For i = 1 To ArrSize
LstBox.AddItem TempFilesArray(i)
Next
 
zigi said:
Hello,

I found the following solution on the web after days of searching:
The way is to put all NOT-selected items in an array, delete the whole
listbox and then fill the listbox aggain with all items from the
array.


Dim ArrSize As Integer
Dim i As Integer
Dim TempFilesArray()

ArrSize = 0
For i = 0 To LstBox.ListCount - 1
If LstBox.Selected(i) = False Then
ArrSize = ArrSize + 1
ReDim Preserve TempFilesArray(ArrSize)
TempFilesArray(ArrSize) = LstBox.ItemData(i)
End If
Next

For i = 0 To LstBox.ListCount - 1
LstBox.RemoveItem (0)
Next i

For i = 1 To ArrSize
LstBox.AddItem TempFilesArray(i)
Next

Here's another approach. I don't know for sure, but I think it might be
marginally more efficient:

Dim strRemoveList As String
Dim astrRemoveItem() As String
Dim varItem As Variant
Dim lngI As Long

With Me.lstSource

For Each varItem In .ItemsSelected
strRemoveList = strRemoveList & Chr(0) & .ItemData(varItem)
Next varItem

If Len(strRemoveList) > 0 Then
astrRemoveItem = Split(Mid$(strRemoveList, 2), Chr(0))

For lngI = LBound(astrRemoveItem) To UBound(astrRemoveItem)
.RemoveItem astrRemoveItem(lngI)
Next lngI

End If

End With
'----- start of code -----
Dim strRemoveList As String
Dim astrRemoveItem() As String
Dim varItem As Variant
Dim lngI As Long

With Me.lstSource

For Each varItem In .ItemsSelected
strRemoveList = strRemoveList & Chr(0) & .ItemData(varItem)
Next varItem

If Len(strRemoveList) > 0 Then
astrRemoveItem = Split(Mid$(strRemoveList, 2), Chr(0))

For lngI = LBound(astrRemoveItem) To UBound(astrRemoveItem)
.RemoveItem astrRemoveItem(lngI)
Next lngI

End If

End With
'----- end of code -----
 
Thank you very much, you have just solved a problem I have been tring to resolve for about 2 weeks.

Jerry S.
 
Back
Top