无法获取Range类的findnext属性

时间:2013-10-20 00:38:05

标签: excel-vba vba excel

我的代码应该在j列中查找一些值,如果找到它们,则删除第1列到第11列之间的行。但是,我总是得到错误“无法获取范围的findnext属性class“associated to”Set c = .FindNext(c)“。有什么问题?感谢!!!

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 个答案:

答案 0 :(得分:1)

这是因为您要删除Find引用的范围。

FindNext向上移动一行以便在删除行之前更新它应该可以正常工作,如下所示:

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