Vba to delete entire row if cell got multiple wildcard conditions

kit99

Active Member
Joined
Mar 17, 2015
Messages
352
I'm trying to delete entire row if cell in column I is NOT like "*loenn*" or not like "*lott*".
Both wildcard search and multiple conditions if to delete row.

Tried this:

Code:
    Last = Cells(Rows.Count, "A").End(xlUp).Row
    For i = Last To 2 Step -1
        If ((Cells(i, "I").Value <> "*loenn*") Or _
            (Cells(i, "I").Value <> "*lott*")) Then
            
            Cells(i, "I").EntireRow.Delete
        End If
    Next i

... but it deletes ALL rows!
Anyone that knows how I can get this vba to work?

And is there perhaps a way to to this faster? Maybe autofilter (I've never used this function)?
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
The "<>" operator is "not equals" you probably need:

Code:
    Last = Cells(Rows.Count, "A").End(xlUp).Row
    For i = Last To 2 Step -1
        If ((Cells(i, "I").Value Like "*loenn*") Or _
            (Cells(i, "I").Value Like "*lott*")) Then
            ' Do Nothing
        Else
            Cells(i, "I").EntireRow.Delete
        End If
    Next i

WBD
 
Upvote 0
The "<>" operator is "not equals" you probably need:

Code:
    Last = Cells(Rows.Count, "A").End(xlUp).Row
    For i = Last To 2 Step -1
        If ((Cells(i, "I").Value Like "*loenn*") Or _
            (Cells(i, "I").Value Like "*lott*")) Then
            ' Do Nothing
        Else
            Cells(i, "I").EntireRow.Delete
        End If
    Next i

WBD

Thanks, but no sigar...
Your vba also deletes every row except headers.
I know for a fact that in column I (column no 9) there are 6 rows holdig value/text "*loenn*" or "*lott*", and these rows I want to keep. Others to be deleted.
 
Upvote 0
I think you need to check the contents of those cells. I ran it locally and it did as expected. Is the text in the cells in lower case? Do you need to make it lower case before running the comparison?

Code:
    Last = Cells(Rows.Count, "A").End(xlUp).Row
    For i = Last To 2 Step -1
        If ((LCase(Cells(i, "I").Value) Like "*loenn*") Or _
            (LCase(Cells(i, "I").Value) Like "*lott*")) Then
            ' Do Nothing
        Else
            Cells(i, "I").EntireRow.Delete
        End If
    Next i

WBD
 
Upvote 0
I think you need to check the contents of those cells. I ran it locally and it did as expected. Is the text in the cells in lower case? Do you need to make it lower case before running the comparison?

Code:
    Last = Cells(Rows.Count, "A").End(xlUp).Row
    For i = Last To 2 Step -1
        If ((LCase(Cells(i, "I").Value) Like "*loenn*") Or _
            (LCase(Cells(i, "I").Value) Like "*lott*")) Then
            ' Do Nothing
        Else
            Cells(i, "I").EntireRow.Delete
        End If
    Next i

WBD


Outch... If my PC could talk it would say: "Error located between keyboard and chair back"!
Your vba Works!
Thanks a lot for helping out. :)
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top