简体   繁体   English

使用 ListBox 通过用户窗体中的按钮从表中删除一行

[英]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.到目前为止,我只能删除 ListBox 项目,但不能删除我的 Excel 工作表中的行。

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:下面是我如何删除 ListBox 项:

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").到目前为止这段代码有效,现在我想删除.Range("Tableau1") 的.Row(i) 的值。

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.更清楚地说,我想删除表(“Tableau1”)的行(i)而不是工作表的整行,因为同一行上还有其他数据。

Any ideas of how to help me?关于如何帮助我的任何想法?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM