Excel VBA delete Listbox item

Asked

Viewed 5,617 times

0

I’m having a hard time deleting an item from a listbox.
The code works like this: will have the listbox with the list, and when the user wants to delete some item, he clicks on a button that opens another userform, in that userform the user inserts the item number to be deleted in the listbox, along with a msgbox to ask for confirmation of the exclusion.

Private Sub CommandButton1_Click()
    Dim curso

    resposta = MsgBox("Deseja excluir o curso?", vbYesNo + vbQuestion, "Excluir?")
    If resposta = vbYes Then
        curso = TextBox1.Text
        UserForm2.ListBox1.RemoveItem (curso)
    End If
End Sub

For functionality reasons, I would prefer the code to work this way, with two different confirmations (explicitly inform the item number to be deleted and the msgbox).

I’m open to opinions.

  • what mistake happens?

  • 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 from ListBox and I tried to adapt them all in my code but none of them worked. !

2 answers

1

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
  • Nice answer, but for it to meet what was asked in the question would have to also have the way to delete an item from the list without it being selected, by the item number!

0

Private Sub CommandButton1_Click()
    ListBox1.AddItem "teste"

End Sub

Private Sub CommandButton2_Click()

    Dim curso

    resposta = MsgBox("Deseja excluir o curso?", vbYesNo + vbQuestion, "Excluir?")
    If resposta = vbYes Then
        curso = TextBox1.Text
        UserForm1.ListBox1.RemoveItem (curso)
    End If

End Sub

I tested with this code here and it worked. The only difference is that the text box that informs the position of the item to be deleted is in the same Userform. inserir a descrição da imagem aqui

  • It didn’t work! Appears "Compilation error - Named argument not located" If I’m not mistaken, I had tried so already! ..RemoveItem that I’m missing!

  • Try taking out this "Userform2." leaves only Listbox.Removeitem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.