Delete Rows based on 2 criteria


Trying to delete rows with a macro. First criteria is that text in a cell does not start with GE WEST and the second criteria is to delete the row if the text in cell D is equal to the Text in Cell A2 on another sheet.

Example included as this is hard to explain.

Edit: The attached example only has a few rows but the working file could have many thousands.

Thanks in advance,

Who is Participating?

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

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.

I placed a value in A3 in Sheet1

Open in new window

Running this code produces the results you desired.
Sub Q_28711679()
    Dim wks As Worksheet
    Dim rng As Range
    Set wks = Worksheets("Sheet2")      'ActiveSheet
    Application.ScreenUpdating = False
    wks.Range("A1").CurrentRegion.AdvancedFilter Action:=xlFilterInPlace, _
        CriteriaRange:=Worksheets("Sheet1").Range("A1:A3"), Unique:=False
    wks.Range(wks.Range("A2"), wks.Cells.SpecialCells(xlCellTypeLastCell)).Cells.SpecialCells(xlCellTypeVisible).EntireRow.Delete
    Application.ScreenUpdating = True
End Sub

Open in new window

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Sub DeleteRows()
Dim ws As Worksheet
Dim lr As Long
Set ws = Sheets("Sheet2")
lr = ws.Cells(Rows.Count, "D").End(xlUp).Row
Application.ScreenUpdating = False
With ws.Rows(1)
    .AutoFilter field:=4, Criteria1:="<>GE WEST*"
    On Error Resume Next
    ws.Range("D2:D" & lr).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    .AutoFilter field:=4, Criteria1:=Sheets("Sheet1").Range("A2").Value
    ws.Range("D2:D" & lr).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
Application.ScreenUpdating = True
MsgBox "Rows based on criteria have been deleted successfully.", vbInformation
End Sub

Open in new window

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
swjtx99Author Commented:
Hi aikimark & sktneer,

Thank you for your help. Both solutions worked but I'm not able modify Sheet1 to put that into A3 so sktneer's was the best answer. I split the points giving the majority to the best solution because both solutions were very similar. Hope you both think that is fair.

Thanks again,

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome. Glad we could help.
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.