2013-10-20 176 views
0

我的代碼應該查找j列中的一些值,如果找到它們,請刪除列1至11之間的行。但是,我總是收到錯誤「無法獲取與」Set c = .FindNext(c)「關聯的Range類的findnext屬性。有什麼問題?謝謝!!!無法獲得Range類的findnext屬性

Sub ExcluirGraosIncompletos() 


    Application.ScreenUpdating = False 

    Dim c As Range 
    Dim ClearAddress As String 
    Dim ClearRow As Long 


    With Worksheets("ListBCOGJ").Range("J4:J17780") 


     Set c = .Find(What:=6, LookIn:=xlValues, LookAt:=xlWhole, _ 
             MatchCase:=True) 
     If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Range(Cells(ClearRow, 1), Cells(ClearRow, 11)).Delete Shift:=xlUp 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 

     Set c = .Find(What:=5, LookIn:=xlValues, LookAt:=xlWhole, _ 
             MatchCase:=True) 
     If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Range(Cells(ClearRow, 1), Cells(ClearRow, 11)).Delete Shift:=xlUp 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 

     Set c = .Find(What:=42, LookIn:=xlValues, LookAt:=xlWhole, _ 
             MatchCase:=True) 
     If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Range(Cells(ClearRow, 1), Cells(ClearRow, 11)).Delete Shift:=xlUp 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 

     Set c = .Find(What:=66, LookIn:=xlValues, LookAt:=xlWhole, _ 
             MatchCase:=True) 
     If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Range(Cells(ClearRow, 1), Cells(ClearRow, 11)).Delete Shift:=xlUp 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 

     Set c = .Find(What:=36, LookIn:=xlValues, LookAt:=xlWhole, _ 
             MatchCase:=True) 
     If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Range(Cells(ClearRow, 1), Cells(ClearRow, 11)).Delete Shift:=xlUp 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 

     Set c = .Find(What:=1, LookIn:=xlValues, LookAt:=xlWhole, _ 
             MatchCase:=True) 
     If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Range(Cells(ClearRow, 1), Cells(ClearRow, 11)).Delete Shift:=xlUp 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 

     Set c = .Find(What:=4, LookIn:=xlValues, LookAt:=xlWhole, _ 
             MatchCase:=True) 
     If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Range(Cells(ClearRow, 1), Cells(ClearRow, 11)).Delete Shift:=xlUp 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 

     Set c = .Find(What:=2, LookIn:=xlValues, LookAt:=xlWhole, _ 
             MatchCase:=True) 
     If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Range(Cells(ClearRow, 1), Cells(ClearRow, 11)).Delete Shift:=xlUp 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 

     Set c = .Find(What:=27, LookIn:=xlValues, LookAt:=xlWhole, _ 
             MatchCase:=True) 
     If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Range(Cells(ClearRow, 1), Cells(ClearRow, 11)).Delete Shift:=xlUp 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 

     Set c = .Find(What:=60, LookIn:=xlValues, LookAt:=xlWhole, _ 
             MatchCase:=True) 
     If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Range(Cells(ClearRow, 1), Cells(ClearRow, 11)).Delete Shift:=xlUp 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 


    End With 

     Application.CutCopyMode = False 
     Range("A1").Select 

    Application.ScreenUpdating = True 

End Sub 

回答

1

這是因爲您正在刪除Find引用的範圍。

移動FindNext了線,使得它的更新前的行被刪除或許應該工作,像這樣:

ClearRow = c.Row 
Set c = .FindNext(c) 
Range(Cells(ClearRow, 1), Cells(ClearRow, 11)).Delete Shift:=xlUp 
+1

確認通過單步執行代碼,用手錶集每當C鈕,刪除c.row將c重置爲Nothing。 –