2016-02-11 162 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 
相关问题