2014-02-14 62 views
0

我必須在2天內完成所有問題。但這裏還有一個。Obj Var or With Block Var未設置

我有一些代碼,有人幫助我,我得到一個對象變量錯誤。

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

不知道擋出原因,只能認爲它可能有一些做的事實,沒有「 - 」被發現。

請幫忙。

+0

不知道你想什麼,在這裏刪除,但一旦你找不到「 - 」(即C是什麼),rngToDelete沒有定義,從而出現錯誤。 – Alex

+0

這就是我想的,如果沒有找到,你有什麼建議來糾正它嗎? – SASUSMC

+0

如果你打算用「 - 」刪除你找到的每一行,我認爲加里的學生有答案〜 – Alex

回答

2

必須顛倒的邏輯:

​​