从ListBox范围多选VBA中删除行,仅删除最后一行

时间:2018-10-17 17:03:40

标签: excel vba listbox

在询问之前,我真的很想找到答案,但是找不到解决方法。

我是VBA的新手,我已尽力而为,但我无法弄清楚,我已经尝试过使用诸如此类的数组,但我做不到,所以如果我的问题的答案已经在另一篇文章中回答,对不起,好吗?我找不到,问题的标题可能不像我所寻找的那么明确...

因此,我试图在用户从列表框中选择某些行后将其删除,但是代码仅删除所选范围内的最后一行。

请帮帮我。谢谢大家。

我的代码是:

Private Sub cmdBDGDeleteExpense_Click()

Dim UserAnswer As Long
Dim i As Long

UserAnswer = MsgBox("Are you sure you would like to delete this expense?", vbCritical + vbYesNo + vbDefaultButton2, "Delete Expense")

If UserAnswer = 6 Then
    For i = lbxBDGExpensesHistory.ListCount - 1 To 0 Step -1
        If lbxBDGExpensesHistory.Selected(i) Then
            Worksheets("expenses_history").Cells(lbxBDGExpensesHistory.ListCount - (lbxBDGExpensesHistory.ListCount - i) + 2, 1).EntireRow.Delete
        End If
    Next i
Else
    MsgBox "Nothing was deleted!"
End If

End Sub

0 个答案:

没有答案