简体   繁体   English

从用户窗体中删除表格行

[英]Delete Table Row From Userform

I have made a userform for deleting a row of data.我制作了一个用于删除一行数据的用户表单。 Is it possible when i delete a row of data, the only row affected is the table row, while the same row outside the table is not deleted?是否有可能当我删除一行数据时,唯一受影响的行是表行,而表外的同一行没有被删除?

For example, if want to remove Bill, test1, and 345, is it possible to make the data1 still remain there and not removed?比如要删除Bill、test1、345,是不是可以让data1还在原处不被删除呢?

例子

I have this code but it delete the entire row, any idea how to modify this?我有这段代码,但它删除了整行,知道如何修改吗?

Private Sub CommandButton5_Click()
Dim profile_id As String
profile_id = ComboBox9.value
Lastrow = Sheets("Profile").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To Lastrow
If Sheets("Profile").Cells(i, 1).value = profile_id Then
Sheets("Profile").Rows(i).Delete
Unload Me
MsgBox "Your data has been deleted", vbOKOnly, "Successful"
End If
Next
End Sub

Something like this will not affect other content on the sheet这样的事情不会影响工作表上的其他内容

ActiveSheet.ListObjects(1).ListRows(3).Delete

Cross-posted: https://www.mrexcel.com/board/threads/remove-table-row-from-userform.1132450/交叉发布: https://www.mrexcel.com/board/threads/remove-table-row-from-userform.1132450/

Answering for posterity.为后人解答。 If you load the data in your userform, I'm assuming into a ListBox control, as an array and not sourced directly to the Table data, then yes you can do that.如果您在用户窗体中加载数据,我假设作为数组而不是直接来自 Table 数据的 ListBox 控件,那么是的,您可以这样做。 You'd basically loop through your listbox data (array), find any selected item, and remove it with the RemoveItem method.您基本上会遍历列表框数据(数组),找到任何选定的项目,然后使用 RemoveItem 方法将其删除。 Here is an example:这是一个例子:

Private Sub CommandButton1_Click()

    Dim Index As Long
    Dim RemoveRow As Long
    Dim Values As Variant

    Values = Me.ListBox1.List
    RemoveRow = -1

    For Index = 0 To Me.ListBox1.ListCount - 1
        If Me.ListBox1.Selected(Index) Then
            RemoveRow = Index
            Exit For
        End If
    Next Index

    If RemoveRow = -1 Then Exit Sub
    Me.ListBox1.RemoveItem RemoveRow

End Sub

If you wanted to actually remove the Table row, and not in the userform only first, you would replace this line:如果您想实际删除 Table 行,而不是仅首先在用户表单中,您将替换此行:

Me.ListBox1.RemoveItem RemoveRow

With these lines:使用这些行:

ThisWorkbook.Worksheets("Profile").ListObjects("Table1").ListRows(RemoveRow + 1).Delete
Me.ListBox1.List = ThisWorkbook.Worksheets("Profile").ListObjects("Table1").DataBodyRange.Value

Again, changing the sheet/Table names to suit.再次,更改工作表/表格名称以适应。

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

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