2016-02-11 163 views
0
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 

我想刪除多行,要做到這一點,我設置範圍rKill和rKill是我要刪除的所有行的聯合。不過,我有以下問題:Excel VBA刪除行對象變量或與塊變量未設置

Object Variable or With block variable not set 

在倒數第二行:

rKill.EntireRow.Delete 

我想是因爲我暗淡rKill的範圍內,我想設置的行成使其應用範圍這個錯誤,但我試過這個:

Set rKill = Rows (a). Range 
Else 
Set rKill = Union (rKill, Rows (a)).Range 

但仍然無法正常工作。

+5

嘗試'如果不rKill是Nothing然後rKill.EntireRow.Delete'。 – Fadi

+0

您想要指定您正在使用的工作表。而不是「行(a)」,使用「表格(」mySheet「)。行(a)」 – OpiesDad

回答

0

你可以試試這個一個實例:

Sub project() 

    Dim Ws As Worksheet 
    Set Ws = ActiveWorkbook.ActiveSheet 

    Dim a As Long 
    a = 3 

    Dim rKill As Range 

    Do Until Ws.Cells(a, 7).Value = "" 
     If (Abs(Ws.Cells(a - 1, 7).Value - Ws.Cells(a, 7).Value) > 5) And (Abs(Ws.Cells(a + 1, 7).Value - Ws.Cells(a, 7).Value) > 5) Then 
      If rKill Is Nothing Then 
       Set rKill = Ws.Range(Rows(a)) 
      End If 
     End If 
     a = a + 2 
    Loop 
    rKill.EntireRow.Delete 

End sub 
相關問題