Delete rows that contain text that correspond to a list of names

NMB311

New Member
Joined
May 28, 2010
Messages
14
I would like to create a macro that will delete rows in my worksheet if they contain certain names. I have a list of about 10 names in another worksheet ("leader names") that sometimes changes. Is there a way to have excel reference that list and delete rows in another worksheet ("data") if they contain any of those names in the list?
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Code:
Sub DeleteRowsWithLeaderNames()

Dim rngLeaderNames As Range
Dim rngData As Range
Dim i As Range, j As Range


Set rngLeaderNames = ThisWorkbook.Worksheets("leader names").Range("leader_names")
Set rngData = ThisWorkbook.Worksheets("data").Range("data")


For Each i In rngLeaderNames
    For Each j In rngData
        If j.Value = i.Value Then
        j.EntireRow.Delete
        End If
    Next j
Next i

End Sub
 
Upvote 0
Thank you for the quick response! I guess I needed to be more specific. The data all exists in column A in the Data sheet. And the values in each cell in the range don't contain the leader names exclusively. There is a lot of data in each cell in addition to the names. So it seems like the "if value = value, then delete" logic won't work. And sure enough I tried it and it didn't. Thanks again though for the help :)
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,736
Members
452,940
Latest member
Lawrenceiow

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