Delete the row from selected item in listbox RRS feed

  • Question

  • Hi,

    i'm trying to delete the entire row of the item(s) selected in my listbox, but the problem is the code deletes the very first row. Here is the code.

    Private Sub Mature_Click()
    Dim I As Long
        With ListBox1
          For I = .ListCount - 1 To 0 Step -1
           If .Selected(I) Then
               .RemoveItem I
               Sheets("Sheet1").Rows(I + 2).EntireRow.Delete
            End If
          Next I
    End With
    End Sub

    Monday, August 10, 2015 10:39 AM

All replies

  • Your code caters for the fact the index of the first row is zero and so it should consistently delete the selected row in the listbox. Assuming your data starts in row 2 on the sheet it will also delete the equivalent sheet row ie .rows(I + 2) where I is zero based index.

    One thing though, if the user attempts to delete the last row, the previous row willl get selected, you code will keep looping until all rows are deleted. suggest include "Exit For" (without quotes) before the End If.

    • Proposed as answer by André Santo Monday, August 10, 2015 12:10 PM
    Monday, August 10, 2015 11:20 AM