【发布时间】:2021-07-27 13:01:10
【问题描述】:
我正在尝试遍历值字段,如果字段值等于变量名,则删除整行。第一次迭代成功,检测到值时删除该行。但是,下一次迭代不起作用,因为与变量值匹配的行不会被删除。
For y = 0 To Me.inventory_selection.ListCount - 1
If Me.inventory_selection.Selected(y) Then
search_loc = Me.inventory_selection.List(y)
With Worksheets("Records").ListObjects("Inventory")
For i = .ListRows.count To 1 Step -1
If .ListColumns("Location").DataBodyRange(i) = search_loc Then
.ListRows(i).Delete 'Shift:=xlUp
End If
Next i
End With
End If
Next y
我认为这可能与被删除的行有关,并且 i 值没有从中断的地方恢复。请告诉我你的想法,谢谢。
【问题讨论】:
-
欢迎来到 SO!我不认为
i索引是问题所在。当您删除一行时,下面所有行的索引都会更改,但这不会干扰您的代码,因为您的 for 循环步骤 -1(顺便说一句做得很好)。我在您的ifor 循环中看不到任何问题。