Search code examples
excelvbalistviewuserform

Clear contents of specific selected listview rows?


I've been trying to create an event for my application.

I want to be able to click on a row and after selecting this row, click on my 'delete button' and delete the row from my listview, and from my spreadsheet.

I have tried using the ClearContent and the Remove properties but neither of them worked and I got different errors such as Type mismatch (Error 13).

These are my code snippets tryouts:

Private Sub BtnDelete_Click()
    
    ListViewEntries.ListItems.SelectedItem.Remove
    
End Sub
Private Sub BtnDelete_Click()
    
    ListViewEntries.SelectedItem = ClearContents
    
End Sub
Private Sub BtnDelete_Click()
    
    ListViewEntries.SelectedItem.ClearContents(i)
    
End Sub

Not sure where the problem is. Can anyone help?


Solution

  • It should be

    Private Sub BtnDelete_Click()        
        ListViewEntries.ListItems.Remove ListViewEntries.SelectedItem.Index        
    End Sub
    

    or

    Private Sub BtnDelete_Click()        
        Call ListViewEntries.ListItems.Remove(ListViewEntries.SelectedItem.Index)
    End Sub
    

    The the call means:

    ListViewEntries.ListItems.Remove(IndexOfItemToRemove)
    

    Take the list box ListViewEntries go into the ListItems and Remove something. Now Remove needs an agrument what to remove. This argument is an index of an item in the list box. Since you want to remove the selected item you need to submit the index of the selected item ListViewEntries.SelectedItem.Index as parameter to the Remove method.