Obj Var或With Block Var未设置

时间:2014-02-14 21:55:43

标签: vba excel-vba excel-2010 excel

我必须在2天内完成所有问题。但是还有一个。

我有一些人帮助我的代码,我收到了一个Object Variable错误。

Dim firstAddress As String
Dim c As Range
Dim rngToDelete As Range

With Worksheets(5).Range("c1:c1500")
   Set c = .Find("-", LookIn:=xlValues)
   If Not c Is Nothing Then
       firstAddress = c.Address
       Do
           If rngToDelete Is Nothing Then
              Set rngToDelete = c
           Else
              Set rngToDelete = Union(rngToDelete, c)
           End If
           Set c = .FindNext(c)
           If c Is Nothing Then Exit Do
       Loop While c.Address <> firstAddress
   End If
End With

If rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete

它在最后一个rngToDelete.EntireRow.Delete阻止了

不确定原因,只能认为它可能与没有找到“ - ”的事实有关。

请帮助。

1 个答案:

答案 0 :(得分:2)

你必须扭转逻辑:

If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete