如果单元格符合条件 excel vba 如何删除行数

How to delete number of rows if cell meets criteria excel vba

如果这 6 行中第一行的单元格 (C20) 包含“#REF!”,我正在寻找一种方法来删除总共 6 行。这是我目前所拥有的:

Sub Button7_Click()

Dim srchRng As Range

Worksheets("Summary").Activate

ActiveWindow.DisplayFormulas = False


Set srchRng = Range("C20:C300")

Dim c As Range
For Each c In srchRng
If c.Formula = "=#REF!" Then
    ActiveWorkbook.Worksheets("Aug").Columns(2).SpecialCells(xlFormulas,     xlErrors).EntireRow.Delete
              
    Exit For
End If
Next

End Sub

这是我的问题的答案:

Sub Button7_Click()

Dim r As Long

Application.ScreenUpdating = False

Worksheets("Summary").Activate

 '   To Loop through rows backwards
   For r = 300 To 20 Step -1
 '     To Check the formula in column C
      If Cells(r, "C").Formula = "=#REF!" Then
 '         To Delete row and 5 rows under it
        Rows(r & ":" & r + 5).Delete
    End If
 Next r

Application.ScreenUpdating = True

MsgBox "Done!"

End Sub

您也可以替换此行:

       Rows(r & ":" & r + 5).Delete

像这样:

       Cells(r, "C").Resize(6, 1).EntireRow.Delete