VBA delete row based on cell value

I need to remove rows that do not have one of an array of service codes.
The service codes, for example, would be PO, SO, E2.
These codes are in Column A.
I do not know what the length of the data set would be.

Thanks!
Euro5Asked:
Who is Participating?
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.

Saurabh Singh TeotiaCommented:
You can run the following code..it will check for values from row-2 and will do what you are looking for...

Sub deldata()
    Dim i As Long
    i = 2
    Do Until i > Cells(Cells.Rows.Count, "A").End(xlUp).Row
        If InStr(1, Cells(i, "a").Value, "PO", vbTextCompare) = 0 And InStr(1, Cells(i, "a").Value, "SO", vbTextCompare) = 0 And InStr(1, Cells(i, "a").Value, "E2", vbTextCompare) = 0 Then
            Rows(i).Delete
        Else
            i = i + 1
        End If
    Loop
End Sub

Open in new window


In your string it will search for PO,SO and E2 and if it doesn't find it..will delete the row..

Saurabh...
0

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
Rgonzo1971Commented:
Hi,

pls try in one go

Sub Macro1()

    ActiveSheet.Range("A1").AutoFilter
    Set myRange = ActiveSheet.Range(Range("A2"), Range("A" & Cells.Rows.Count))
    myRange.AutoFilter Field:=1, Criteria1:=Array("E2", _
        "PO", "SO"), Operator:=xlFilterValues
    myRange.SpecialCells(xlCellTypeVisible).EntireRow.Delete
    ActiveSheet.Range("A1").AutoFilter
End Sub

Open in new window

Regards
0
Rob HensonFinance AnalystCommented:
Does it have to be done in VBA?

The routine that RGonzo has given above will delete the headers as well, ie everything that is visible.

The routine that he has given uses the Auto Filter process. When a list is Filtered, only those cells that remain visible will be affected by any changes, including deleting of rows. Therefore, if you apply a filter and then as a block select the rows you want rid of, only those will be deleted.

Thanks
Rob H
0
Euro5Author Commented:
Thank you!!
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.