Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

excel

vba

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.

like image 742
EmptyWolf Avatar asked Feb 03 '26 03:02

EmptyWolf


1 Answers

You can use "like" instead of "=".

 If .Value like "Jo*" _
     Or .Value like "*err*" _
like image 138
Yuxiang Yang Avatar answered Feb 04 '26 17:02

Yuxiang Yang