Home » excel » excel – Deleting an entire row based on the text in a cell of that row

excel – Deleting an entire row based on the text in a cell of that row

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am trying to use VBA in order to delete an entire row based on the approximate content of a cell of that same row. I have found this for the exact content:

Sub DeleteRowBasedOnOwner()
    Dim RowToTest As Long
    For RowToTest = Cells(Rows.Count, 5).End(xlUp).Row To 2 Step -1
        With Cells(RowToTest, 5)
            If .Value = "John" _
            Or .Value = "Jerry" _
            Or .Value = "Steve" _
            Or .Value = "Robert" _
            Or .Value = "Sarah" _
            Or .Value = "Leonard" _
            Or .Value = "Darryl" _
            Or .Value = "Mike" _
            Or .Value = "Martin" _
            Then _
            Rows(RowToTest).EntireRow.Delete
        End With
    Next RowToTest
End Sub

However, wildcards don’t seem to work here. For instance, I’ve tried:

If .Value = "Jo*" _
Or .Value = "*err*" _

But this does not work. I Hope someone can help me with this.

How to&Answers:

You can use “like” instead of “=”.

 If .Value like "Jo*" _
     Or .Value like "*err*" _