attempting to delete rows not containing certain text but having issues.

jfitzsimmons

New Member
Joined
Jun 11, 2019
Messages
1
Basically im trying to filter a large set of data. I have people's names and want to delete all rows not containing these names. I used this macro originally:

Sub DeleteRows()
Dim Cell As Range, cRange As Range, LastRow As Long, x As Long
LastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
Set cRange = Range("C2:C" & LastRow)
For x = cRange.Cells.Count To 1 Step -1
With cRange.Cells(x)
If .Value <> "john smith" And .Value <> "jane doe" Then
.EntireRow.Delete
End If
End With
Next x
End Sub


The problem was, some of the names also included middle initial or some difference. Is there a way to make this not specific. so if I put If .Value <> "John Smith" it can keep all cells containing that, not equaling that?
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I think you want to turn the problem around to KEEP the names you have.
Look at the Advanced Filter using the Copy to a new location.
 
Upvote 0
Try replacing this line:

If .Value <> "john smith" And .Value <> "jane doe" Then

with this:
If Not .Value Like "*John*Smith" And Not .Value Like "*Jane*Doe" Then
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

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