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

时间:2019-02-19 20:47:44

标签: excel vba row delete-row

我创建了一个宏,以便生成每日报告。宏的一部分可在AN列中找到一个值并删除整个行(已编辑代码以删除从最后使用的行开始的行),效果很好。 下面的示例删除AN列中所有不包含值“ CAT”,“ BAT”或“ 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来删除要做包含一组特定值的所有行。 我尝试用 = == 替换<> ,但是没有用,也没有删除行

4 个答案:

答案 0 :(得分:1)

下面是一个如何根据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

答案 1 :(得分:1)

感谢大家对解决此问题的帮助。我发现问题的根本原因仅仅是If / Then行末的条件语句。 “ And _ ”语句的意思是“如果单元格等于CAT,BAT和DOG,则删除行”,而不是“如果单元格等于CAT,BAT或DOG,则删除行”。将“ 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

谢谢!

答案 2 :(得分:0)

我倾向于这样做:

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

答案 3 :(得分:0)

以下网站可能会为您提供帮助。

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