Share via

VBA Code to delete rows based on single criteria

Anonymous
2020-01-24T14:02:55+00:00

I have data in columns A through AC, with some columns being blank.  Column Headings are in row 1

I would like to delete rows if "Y" appears in column "X".  Worksheet name is "List" and has about 3,000 lines, but varies day to day.

I've tried codes that loop but they take forever.  I also have the following code, but it gives me an error message at the .SpecialCells(xlCellTypeConstants).EntireRow.Delete line.  I then just kept the .EntireRow.Delete for that line and it still did not work.  Any help would be greatly appreciated.

Dim UnusedColumn As Long, LastRow As Long

    UnusedColumn = Cells(1, Columns.Count).End(xlToLeft).Column + 1

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

    Application.ScreenUpdating = False

    Application.Calculation = xlCalculationManual

    Range("X2:X" & LastRow).Copy Cells(2, UnusedColumn)

    With Columns(UnusedColumn)

    .Replace "N", "", xlWhole

    .SpecialCells(xlCellTypeConstants).EntireRow.Delete

    .Clear

    End With

    Application.Calculation = xlCalculationAutomatic

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. HansV 462.6K Reputation points MVP Volunteer Moderator
    2020-01-24T14:27:25+00:00

    Try this:

    Sub DeleteRowsWithX()

        Dim m As Long

        With Worksheets("List")

            .Range("X:X").AutoFilter Field:=1, Criteria1:="Y"

            m = .Range("X" & .Rows.Count).End(xlUp).Row

            If m > 1 Then

                .Range("X2:X" & m).EntireRow.Delete

            End If

            .Range("X:X").AutoFilter

        End With

    End Sub

    6 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2020-01-24T15:05:55+00:00

    Worked great!!  Thank you!

    1 person found this answer helpful.
    0 comments No comments