Code deletes first row/headers.

J

J.W. Aldridge

Code works, but keeps deleting the headers in row 1. How to avoid/fix
please?

Sub Delete_ROUTES_ColE()

Application.ScreenUpdating = False

Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long

With ActiveSheet

'Set the first and last row to loop through
Firstrow = .UsedRange.Cells(2).Row 'The 1 represents the
RowIndex
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

'Loop from Lastrow to Firstrow (bottom to top)
For Lrow = Lastrow To Firstrow Step -1

'Check the values in the D column
With .Cells(Lrow, "E")


If .Value <> "APPLES" And .Value <> "GRAPES" And .Value <>
"PLUMS" And .Value <> "DATES" And .Value <> "PEARS"
Then .EntireRow.Delete

End With
Next Lrow

End With

Application.ScreenUpdating = True

End Sub
 
D

Don Guillett Excel MVP

Code works, but keeps deleting the headers in row 1. How to avoid/fix
please?

Sub Delete_ROUTES_ColE()

Application.ScreenUpdating = False

        Dim Firstrow As Long
        Dim Lastrow As Long
        Dim Lrow As Long

        With ActiveSheet

        'Set the first and last row to loop through
        Firstrow = .UsedRange.Cells(2).Row 'The 1 represents the
RowIndex
        Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

        'Loop from Lastrow to Firstrow (bottom to top)
        For Lrow = Lastrow To Firstrow Step -1

        'Check the values in the D column
        With .Cells(Lrow, "E")

            If .Value <> "APPLES" And .Value <> "GRAPES" And ..Value <>
"PLUMS" And .Value <> "DATES" And .Value <> "PEARS"
Then .EntireRow.Delete

        End With
        Next Lrow

    End With

Application.ScreenUpdating = True

End Sub

I would have done it differently but instead of cells(2), use
cells(2,1) or range("a2")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top