Excel VBA使用零,错误的运算符删除行?

时间:2018-03-29 04:08:59

标签: excel-vba vba excel

我正在尝试通过删除列C包含零的任何行来清理我的电子表格。

代码运行但零仍然存在。我应该使用Is Nothing吗?

我如何定义它?

    Sub CleanUp()

Dim sh As Worksheet
Dim Oppo As Long
Dim CheckLoop As Long
Dim RowCount As Long ' <-- define RowCount as Long
Dim DelRng As Range

Set sh = ThisWorkbook.Sheets("Database")

With sh
    RowCount = .Cells(.Rows.Count, "C").End(xlUp).Row ' get last row with data in column "C"

    For CheckLoop = RowCount To 2 Step -1
        If .Range("C" & CheckLoop).Value = 0 Then ' <== modified this line
            If Not DelRng Is Nothing Then
                Set DelRng = Application.Union(DelRng, .Rows(CheckLoop))
            Else
                Set DelRng = .Rows(CheckLoop)
            End If
        End If
    Next CheckLoop
End With
' if range object is not empty >> delete entire rows at 1-line
If Not DelRng Is Nothing Then DelRng.Delete

End Sub

提前致谢

编辑:

1 个答案:

答案 0 :(得分:1)

您可以添加DelRng对象,使用Union合并符合条件的所有行,而不是逐行删除,最后只需使用{一次删除所有行{1}}。

更多解释为什么您在下面的代码中出现错误,作为评论。

<强>代码

DelRng.Delete