Sub project()
Dim a As Long
Dim rKill As Range
a = 3
Do
If Abs(Cells(a - 1, 7).Value - Cells(a, 7).Value) > 5 And Abs(Cells(a + 1, 7).Value - Cells(a, 7).Value) > 5 Then
If rKill Is Nothing Then
Set rKill = Rows(a)
Else
Set rKill = Union(rKill, Rows(a))
End If
End If
a = a + 2
Loop Until Cells(a, 7).Value = ""
rKill.EntireRow.Delete
End sub
I am trying to delete multiple rows, to do that, I set a range rKill and rKill is a union of all rows I am going to delete. However, I am having following problems:
Object Variable or With block variable not set
on the second last line:
rKill.EntireRow.Delete
I am thinking because I dim rKill as range, and I am trying to set rows into the range which make this error, but I tried this:
Set rKill = Rows (a). Range
Else
Set rKill = Union (rKill, Rows (a)).Range
but still doesn't work.
If Not rKill Is Nothing Then rKill.EntireRow.Delete
. – Fadi