从列表框中的所选项目中删除行 [英] Delete the row from selected item in listbox
问题描述
我正在尝试删除列表框中所选项目的整行,但问题是代码删除了第一行。这是代码。
i'm trying to delete the entire row of the item(s) selected in my listbox, but the problem is the code deletes the very first row. Here is the code.
Private Sub Mature_Click()
Dim I As Long
With ListBox1
For I = .ListCount - 1 To 0 Step -1
If .Selected(I) Then
.RemoveItem I
Sheets("Sheet1").Rows(I + 2).EntireRow.Delete
End If
Next I
End With
End Sub
推荐答案
您的代码满足第一行索引为零的事实,因此应始终删除列表框中的选定行。假设你的数据在表格的第2行开始,它也会删除等价的表格行,即.rows(I + 2),其中
我是基于零的索引。
Your code caters for the fact the index of the first row is zero and so it should consistently delete the selected row in the listbox. Assuming your data starts in row 2 on the sheet it will also delete the equivalent sheet row ie .rows(I + 2) where I is zero based index.
有一点但是,如果用户尝试删除最后一行,则前一行将被选中,您的代码将保持循环,直到删除所有行。建议包括"退出"和"退出"。 (在没有引号的情况下)在结束之前。
One thing though, if the user attempts to delete the last row, the previous row willl get selected, you code will keep looping until all rows are deleted. suggest include "Exit For" (without quotes) before the End If.
这篇关于从列表框中的所选项目中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!