To select one item at a time
This code removes the selected item
'''
'Remover item
'''
'Remove item selecionado da lista 1
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
ListBox1.RemoveItem (i)
End If
Next i
Clear Listbox
Me.ListBox1.Clear
Confirmation before deleting the item
Private Sub CommandButton1_Click()
'''
'Remover item
'''
Dim counter As Integer
'Remove item selecionado da lista 1
With Me
For i = 0 To .ListBox1.ListCount - 1
If .ListBox1.Selected(i) Then
resposta = MsgBox("Deseja excluir o item: " & .ListBox1.List(i) & "?", vbYesNo + vbQuestion, "Excluir?")
If vbYes Then .ListBox1.RemoveItem (i)
End If
Next i
End With
End Sub
Multi-selection
There are three options of Multiselect
:
ListBox.MultiSelect = 0
: Select only one element. (Item removal methods explained above)
ListBox.MultiSelect = 1
: Click the item or press the space bar to select multiple items
ListBox.MultiSelect = 2
: Press Shift and Ctrl to select multiple items
Code:
Private Sub UserForm_Initialize()
Me.ListBox1.MultiSelect = Número_Multiselect
End Sub
Remove/Delete Multiple items with individual confirmation
Private Sub CommandButton1_Click()
'''
'Remover item
'''
'Remove item selecionado da lista 1
With Me
For i = .ListBox1.ListCount - 1 To 0 Step -1
If .ListBox1.Selected(i) Then
resposta = MsgBox("Deseja excluir o item: " & .ListBox1.List(i) & "?", vbYesNo + vbQuestion, "Excluir?")
If vbYes Then .ListBox1.RemoveItem (i)
End If
Next i
End With
End Sub
Remove/Delete Multiple items with multiple confirmation
Private Sub CommandButton1_Click()
'''
'Remover item
'''
'Remove item selecionado da lista 1
Dim i As Long, contador As Long
Dim vetor() As Variant
With Me
For i = .ListBox1.ListCount - 1 To 0 Step -1
If .ListBox1.Selected(i) Then
ReDim Preserve vetor(contador)
msg = msg & vbCrLf & .ListBox1.List(i)
vetor(contador) = i
contador = contador + 1
End If
Next i
resposta = MsgBox("Deseja excluir os itens a seguir? " & msg, vbYesNo + vbQuestion, "Excluir?")
If vbYes Then
For i = LBound(vetor) To UBound(vetor)
.ListBox1.RemoveItem (vetor(i))
Next i
End If
End With
End Sub
By Item Number:
In case you want to remove by item number, be aware that the list starts at zero.
For an example where Listbox is in Userform1 and the entry of the item number is in Userform2, with a command button and a text box:
Userform1
Private Sub CommandButton1_Click()
UserForm2.Show
End Sub
Userform2
Private Sub CommandButton1_Click()
'''
'Remover item
'''
'Remove item selecionado da lista 1
With UserForm1
i = Me.TextBox1
resposta = MsgBox("Deseja excluir o item: " & .ListBox1.List(i) & "?", vbYesNo + vbQuestion, "Excluir?")
If vbYes Then .ListBox1.RemoveItem (i)
End With
Me.Hide
Unload Me
End Sub
List starting at 1
If you want the list to start at 1, the Userform2 code is as follows:
Private Sub CommandButton1_Click()
With UserForm1
i = Me.TextBox1
resposta = MsgBox("Deseja excluir o item: " & .ListBox1.List(i - 1) & "?", vbYesNo + vbQuestion, "Excluir?")
If vbYes Then .ListBox1.RemoveItem (i - 1)
End With
Me.Hide
Unload Me
End Sub
what mistake happens?
– lcssanches
Appears as "Unspecified Error" and hangs on the line of
ListBox.RemoveItem
. Searching the net, I found many examples of codes that were deleted when the item was selected. But as I said, it’s not the way I want to do it. I ended up finding some 3 different ways to use the.RemoveItem
and delete an item fromListBox
and I tried to adapt them all in my code but none of them worked. !– Henrique Yuji Kamiya