Delete rows based on not equal to with wild card

Apple08

Active Member
Joined
Nov 1, 2014
Messages
450
Hi All

I need to delete some rows which are not equal to certain text including wildcard as words can be attached with other text as well. However the macro doesn't work, please could anyone help? Thanks.


VBA Code:
Private Sub RemoveRows()

    Sheet1.Select
    Dim x As Long, LastRow As Long

    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
    Range("B:B").Delete

    For x = LastRow To 2 Step -1
        If Cells(x, 2).Value <> "*Apple*" And Cells(x, 2).Value <> "*Orange*" And Cells(x, 2).Value <> "*Banana*" Then
            Rows(x).Delete
        End If
       
    Next x


End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try changing this line:
VBA Code:
If Cells(x, 2).Value <> "*Apple*" And Cells(x, 2).Value <> "*Orange*" And Cells(x, 2).Value <> "*Banana*" Then
to this:
VBA Code:
If InStr(LCase(Cells(x, 2)), "apple") = 0 And InStr(LCase(Cells(x, 2)), "orange") = 0 And InStr(LCase(Cells(x, 2)), "banana") = 0 Then
 
Upvote 1
Solution
If InStr(LCase(Cells(x, 2)), "apple") = 0 And InStr(LCase(Cells(x, 2)), "orange") = 0 And InStr(LCase(Cells(x, 2)), "banana") = 0 Then
Thanks Joe4, it works perfectly! Please is the macro case sensitive as I noticed you have put all words in lower case. Thanks.
 
Upvote 0
The "LCase" function takes the value and changes it all to lower case. I did this so the code is NOT case sensitive.
It will work with values like "Apple", "APPLE", or "apple", etc.
 
Upvote 0
The "LCase" function takes the value and changes it all to lower case. I did this so the code is NOT case sensitive.
It will work with values like "Apple", "APPLE", or "apple", etc.
Thanks for the explaination.
 
Upvote 0

Forum statistics

Threads
1,214,620
Messages
6,120,554
Members
448,970
Latest member
kennimack

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