如果单元格等于一组值,则删除行

Delete row if cells equal a set of values

我创建了一个宏来生成每日报告。在 AN 列中找到一个值并删除整行的宏部分(编辑代码以删除从最后使用的行开始的行),效果很好。 以下示例删除 包含值 "CAT"、"BAT" 或“AN 列中的 DOG”的所有行。

'False screen updating
  Application.ScreenUpdating = False
'deleting all other types other than CAT from "samples" tab (excluding the header row, row 1)
  Sheets("sample").Select
  Lastrow = Cells(Rows.Count, "AN").End(xlUp).Row
'Deleting rows from bottom up
    For i = Lastrow To 2 Step -1
        If Range("AN" & i).Value <> "CAT" And _
           Range("AN" & i).Value <> "BAT" And _
           Range("AN" & i).Value <> "DOG" Then
             Rows(i).EntireRow.Delete
        End If
    Next i

但是,想要创建另一个 Sub 来删除 包含一组特定值的所有行。 我尝试 将 <> 替换为 ===,但是两者都没有用,也没有行被删除

我倾向于这样做:

Sub DeleteRows()
    Dim i As Integer
    Dim sht As Worksheet
    Set sht = ThisWorkbook.Sheets("sample")
    i=1

    While sht.(i,1) <> "" 'Assuming first column is full of data to the bottom
        If sht.Range("AN" & i) = "CAT" Then
              sht.Rows(i).EntireRow.Delete
        Else
            i=i+1
        End If
    Wend
End Sub

一个可以帮助您实现以下目标的网站。

https://www.excelcampus.com/vba/delete-rows-cell-values/ 我稍微调整了代码。

Sub Delete_Rows_Based_On_Value()
'Apply a filter to a Range and delete visible rows
'Source: https://www.excelcampus.com/vba/delete-rows-cell-values

    Dim ws As Worksheet

      'Set reference to the sheet in the workbook.
      Set ws = ThisWorkbook.Worksheets("sampel")
      ws.Activate 'not required but allows user to view sheet if warning message appears

      'Clear any existing filters
      On Error Resume Next
        ws.ShowAllData
      On Error GoTo 0

      '1. Apply Filter
      ws.Range("AN3:BG1000").AutoFilter Field:=1, Criteria1:="<>CAT"

      '2. Delete Rows
      Application.DisplayAlerts = False
        ws.Range("B1:G1000").SpecialCells(xlCellTypeVisible).Delete
      Application.DisplayAlerts = True

      '3. Clear Filter
      On Error Resume Next
        ws.ShowAllData
      On Error GoTo 0

    End Sub

下面是一个示例,说明如何根据 A 列中的条件删除行。请记住,如果我们删除行,则会向后移动以避免索引错误。

尝试:

Option Explicit

Sub test()

    Dim Lastrow As Long, i As Long

    With ThisWorkbook.Worksheets("Sheet1")

        Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row

        'Where you delete you go backwards
        For i = Lastrow To 2 Step -1
            If .Range("A" & i).Value = "CAT" Then
                .Rows(i).EntireRow.Delete
            End If

        Next i

    End With

End Sub

感谢大家帮忙解决这个问题。我发现问题的根本原因只是 If/Then 行末尾的条件语句。 “And_”语句是说 "If cell equals CAT and BAT and DOG, then delete row" 而不是 "If cell equals CAT or BAT or DOG, then delete row"。将“And_”替换为“Or_”已解决此问题。

'False screen updating
  Application.ScreenUpdating = False
'deleting all other types other than CAT from "samples" tab (excluding the header row, row 1)
  Sheets("sample").Select
  Lastrow = Cells(Rows.Count, "AN").End(xlUp).Row
'Deleting rows from bottom up
    For i = Lastrow To 2 Step -1
        If Range("AN" & i).Value = "CAT" Or _
           Range("AN" & i).Value = "BAT" Or _
           Range("AN" & i).Value = "DOG" Or _
           Range("AN" & i).Value = "" Then
             Rows(i).EntireRow.Delete
        End If
    Next i

但是,如果单元格是空白“”,我也想删除行。为什么 Sub 会忽略这一行?

  Range("AN" & i).Value = "" Then

谢谢!