Macro to delete rows if they do not contain specific text

  • Hello,


    I am currently working on a macro that will scan columns "E" and "I" and if they do not contain "Inserted" or "ClassID" the row should be deleted. I've tried multiple different variations but can't seem to get it right.. below is what I have so far:


    Sub EvaluateD()
    Dim i, LastRow
    Dim UserIDModLog


    UserIDModLog = ActiveSheet.Name


    LastRow = UserIDModLog(Rows.Count).End(xlUp).Row
    For i = LastRow To 1 Step -1
    If UserIDModLog.Value <> "Inserted" Or "ClassID" Then
    Else
    UserIDModLog.EntireRow.Delete
    End If
    Next i
    End Sub



    Help :(

  • We need some clarification. Do you want to look for "Inserted" in column E and look for "ClassID" in column I or can the words be in either column? Please clarify in detail. If you could attach a copy of your file (de-sensitized if necessary), it would be easier to test a possible solution.

    You can say "THANK YOU" for help received by clicking the "Like" icon in the bottom right corner of the helper's post.
    Practice makes perfect. I am very far from perfect so I'm still practising.

  • i have just started to learn but here what i wrote, there were extra spaces at the end of CLASSID so i had to use trim command, this works just tested it but the gurus of this forum might have a shorter and more precise way to do it.

  • sorry my bad, check this out if i understand correctly


    [ATTACH=JSON]{"data-align":"none","data-size":"medium","data-attachmentid":1223643}[/ATTACH]

  • Try:

    Code
    Sub DeleteRows()
        Application.ScreenUpdating = False
        Dim LastRow As Long
        LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Range("A1:K" & LastRow).AutoFilter Field:=5, Criteria1:="<>Inserted"
        Range("A1:K" & LastRow).AutoFilter Field:=9, Criteria1:="<>ClassID"
        Range("A2:K" & LastRow).EntireRow.SpecialCells(xlCellTypeVisible).Delete
        Range("A1").AutoFilter
        Application.ScreenUpdating = True
    End Sub

    You can say "THANK YOU" for help received by clicking the "Like" icon in the bottom right corner of the helper's post.
    Practice makes perfect. I am very far from perfect so I'm still practising.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!