提问人:sera 提问时间:11/16/2023 最后编辑:sera 更新时间:11/17/2023 访问量:128
同时从用户窗体和电子表格中删除项目
Remove items from userform and spreadsheet at the same time
问:
我试图删除用户表单和电子表格中的项目,但是当我单击删除按钮时,最后一项(纸质传输文件(黄色))在用户表单中删除,但它仍在电子表格中。不知道出了什么问题。
用户窗体中的项目
在用户窗体中删除的项目
工作表中保留的最后一项
请参阅下面的代码。
Dim s As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Data")
Dim selectedItems As New Collection
For s = s To Me.ListboxResult.ListCount - 1
If Me.ListboxResult.Selected(s) Then
selectedItems.Add s
End If
Next s
For s = selectedItems.Count To 1 Step -1
Me.ListboxResult.RemoveItem selectedItems(s)
ws.Rows(selectedItems(s) + 4).Delete
MsgBox "The selected item(s) Is/are successfully removed.", vbOKOnly, "Removal Successful"
Next s
'Uncheck the checkbox if the listbox is empty
If Me.ListboxResult.ListCount = 0 Then
Me.checkboxSelect.Value = False
MsgBox "The selected item(s) Is/are successfully removed.", vbOKOnly, "Removal Successful"
End If
我在下面有这个代码
For Each Cell In rng
With Cell
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
End With
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
End With
End With
Next Cell
我希望该项目在用户窗体和电子表格中都被删除
答:
0赞
taller
11/16/2023
#1
Me.ListBox1.Selected(s)
index 为零基数。的索引是 。paper transfer file(yellow)
2
paper transfer file(yellow)
位于工作表上的单元格中E7
- 您需要按如下方式更改代码。
Debug.Print selectedItems(s) + 5 ' for testing to validate the row#
ws.Rows(selectedItems(s) + 5).Delete
更新:
问题:因为每次我添加到电子表格中的项目都不相同
Dim c As Range
For s = SelectedItems.Count To 1 Step -1
Set c = ws.Columns("E").Find(Me.ListBox1.List(SelectedItems(s)), LookIn:=xlValues, lookat:=xlWhole)
If Not c Is Nothing Then
c.EntireRow.Delete
Set c = Nothing
MsgBox "The selected item(s) Is/are successfully removed.", vbOKOnly, "Removal Successful"
End If
Me.ListboxResult.RemoveItem SelectedItems(s)
Next s
评论
0赞
taller
11/16/2023
你改变了什么?唯一的变化是 .将数字从 更改为 。ws.Rows(selectedItems(s) + 5).Delete
4
5
0赞
sera
11/16/2023
现在我无法删除电子表格中的项目。我确实从 4 变成了 5。
0赞
sera
11/16/2023
我想我也有同样的问题,即使我从 4 改为 5
0赞
taller
11/16/2023
添加为我修改后的代码以仔细检查行#。ListBox1 中项的顺序应与工作表上的项顺序相同。Debug.Print
0赞
sera
11/16/2023
好的,我输入了我的代码,我尝试添加项目删除它们,即时窗口显示 6,5,5debug.print
2赞
CDP1802
11/16/2023
#2
当您发现行处于选中状态时,将其删除。
Option Explicit
Private Sub CommandButton1_Click()
Dim rng As Range, i As Long, c As Range
Dim colItem As Long, sItem As String
colItem = 1 ' 2nd column in listbox
Set rng = ThisWorkbook.Sheets("Data").Range("E:E")
With ListBoxResult
For i = .ListCount To 1 Step -1
If .Selected(i - 1) Then
sItem = .List(i - 1, colItem)
Set c = rng.Find(sItem, LookIn:=xlValues, lookat:=xlWhole)
If Not c Is Nothing Then
c.EntireRow.Delete
Set c = Nothing
Else
MsgBox sItem & " not found on sheet", vbExclamation
End If
.RemoveItem i - 1
End If
Next
End With
End Sub
评论
0赞
sera
11/16/2023
我认为这段代码每次都会让我的程序崩溃。Idk 为什么
0赞
CDP1802
11/16/2023
@Learner11 有什么错误信息吗?
0赞
sera
11/16/2023
不,它只是在单击删除按钮后崩溃了
0赞
CDP1802
11/17/2023
@leaner注释掉,看看列表框删除是否有效。确保你有线。c.EntireRow.Delete
Option Explicit
0赞
sera
11/17/2023
我想我只是想通了什么。请参阅上面我编辑的代码。这也许是它崩溃的原因。但是我想在每次删除项目时在下面添加一个新行。
评论
paper transfer file(yellow)