Search code examples
vbauserformlistobject

Delete a row from a table thanks to a button in a UserForm using ListBox


I'm looking for a way to delete a row of my table at the same time as an item in my ListBox. So far I'm only able to delete the ListBox Item but not the row in my Excel Sheet.

Here is how I initialize my ListBox :

Private Sub UserForm_Initialize()
  with ThisWorkbook.Worksheets(2).Range("Tableau1")
     Me.ListBox1.List = Application.Transpose(.Cells)
  End with
End Sub

Here is how I remove the ListBox item :

Private Sub CommandeButton1_Click()
  With Me.ListBox1
    For i = .ListCount - 1 To 0 Step - 1
      If .Selected(i) Then
        .RemoveItem (i)
      End If
    Next i
 End With
End Sub

So far this code work, now I want to remove the value of the .Row(i) of .Range("Tableau1").

To be more clear I want to remove the row(i) of my table ("Tableau1") but not the full row of the sheet, because there is other data on the same row.

Any ideas of how to help me ?


Solution

  • Private Sub CommandeButton1_Click()
     With Me.ListBox1
      For i = .ListCount - 1 To 0 Step - 1
       If .Selected(i) Then
        .RemoveItem (i)
        ThisWorkbook.Worksheets(2).ListObjects("Tableau1").DataBodyRange(i+1, 1).Delete
       End If
     Next i
    End With
    End Sub