Like Function Not Working for All Cells

dannie

New Member
Joined
Dec 29, 2014
Messages
34
Hello,

I am using the Like function, but it isn't working on all of the cells.
column B has company names. I am trying to get rid of names that have the word sample in them.
my code:
'getting rid of sample plans
For Each cell In Range("B2:B" & lastrow)
If cell Like "*Sample*" Then
cell.EntireRow.Delete
End If
Next

it gets rid of only some of the plans with the word sample. any help is greatly appreciated!

Thanks,
D
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try it with a filter instead of looping, plus, when deleting, you need to work backwards in a loop as the rows will always change.

Code:
Sub DelRows2()
    Application.ScreenUpdating = False
    With Range("B1", Range("B" & Rows.Count).End(xlUp))
        .AutoFilter Field:=1, Criteria1:="*Sample*"
        .Offset(1).EntireRow.Delete
        .AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
You need to loop backwards when deleting rows. You can't use a For Each ... Next construct or some rows will be skipped.
 
Upvote 0
thanks guys!! worked great. that makes this and other projects much simpler. i appreciate it!
D
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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