条件满足时如何在excel中删除整行

我正在研究的程序涉及阅读和确定i – (i + 1)和i-(i-1)excel之间的差异。 如果差异超过4,程序将删除i处的行。 该程序在第一次尝试中运行良好。 但是,当我再次重复它开始删除不应删除的点。

选项显式

Sub Data_Delet()

Dim a As Double, b As Double, c As Double, i As Double Dim rkill As Range ' a,b, and c are used as steps in order to proceed to the next data points a = 18 b = 0 c = 0 With ThisWorkbook.Worksheets("Sheet1") ' The second do loop delete data points that does not follow the requirements Do If Abs(.Cells(a - 1, 2) - .Cells(a, 2)) > 4 And Abs(.Cells(a, 2) - .Cells(a + 1, 2)) > 4 Then If rkill Is Nothing Then Set rkill = Rows(a) Else Set rkill = Union(rkill, Rows(a)) End If End If a = a + 1 Loop Until .Cells(a, 2).Value = "" rkill.EntireRow.Delete End With 

结束小组

更改

 rkill.EntireRow.Delete 

 If Not rkill is Nothing Then rkill.EntireRow.Delete 

此代码检查范围rkill是否是一个range ,然后删除它。 如果不是一个范围,那么它将不会执行后一部分。

另一种方法是(我不build议)

 On Error Resume Next rkill.EntireRow.Delete On Error GoTo 0