Macro to remove rows getting error

The code below works great for removing numbers.
However, when it gets to a row with #N/A or #NAME? it stops and gets an error.
Can someone help? Thanks!!

Sub deleterows()
Application.ScreenUpdating = False

    Dim ws As Worksheet
    Dim lcol As Long, lrow As Long
    Set ws = Sheets("Filtered_Data")

    lrow = 2

    lcol = Range("1:1").Find(What:="Zone", SearchOrder:=xlByColumns, SearchDirection:=xlNext).Column

    Do Until lrow > Cells(Cells.Rows.Count, "A").End(xlUp).Row
Application.StatusBar = "Working on Row Number--> " & lrow
        If (Cells(lrow, lcol).Value < 2 Or Cells(lrow, lcol).Value > 8) Or Trim(Cells(lrow, lcol).Value) = "" Then
            Rows(lrow).Delete
        Else
            lrow = lrow + 1
        End If

    Loop

Application.StatusBar = ""
End Sub

Open in new window

Euro5Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
How about adding On error Resume Next after line 13?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nutschCommented:
You can run the following lines first, between the lcol definition and your loop:

On Error Resume Next

With Range(Cells(2, lcol), Cells(ActiveSheet.Rows.Count, lcol).End(xlUp))
    .SpecialCells(xlCellTypeFormulas, 16).EntireRow.Delete
    .SpecialCells(xlCellTypeConstants, 16).EntireRow.Delete
End With

On Error GoTo 0

Open in new window


Thomas
nutschCommented:
You can also avoid doing a loop altogether and speed up the process. Also you set a reference to     Sheets("Filtered_Data") but you don't use it at all in your code. Watch out if you plan to cross worksheets in your code.

Sub deleterows()
Application.ScreenUpdating = False

    Dim ws As Worksheet
    Dim lcol As Long, lrow As Long
    Set ws = Sheets("Filtered_Data")

    lrow = 2

    lcol = Range("1:1").Find(What:="Zone", SearchOrder:=xlByColumns, SearchDirection:=xlNext).Column

On Error Resume Next

With Range(Cells(2, lcol), Cells(ActiveSheet.Rows.Count, lcol).End(xlUp))
    .SpecialCells(xlCellTypeFormulas, 16).EntireRow.Delete
    .SpecialCells(xlCellTypeConstants, 16).EntireRow.Delete
End With

With Range(Cells(1, lcol), Cells(ActiveSheet.Rows.Count, lcol).End(xlUp))
    .AutoFilter
    .AutoFilter field:=1, Criteria1:="<2", Operator:=xlOr, Criteria2:=">8"
    .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    .AutoFilter field:=1, Criteria1:="="
    .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    .AutoFilter
End With

On Error GoTo 0

Application.ScreenUpdating = True
End Sub

Open in new window

Euro5Author Commented:
THANK YOU!! :)
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.