VBA macro to delete row if column contains same text from another sheet

jo206

New Member
Joined
Jul 10, 2023
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am looking for a macro that can delete rows in Sheet 2 Column A if words "apple juice" or "cookie" are found, but those two words are on Sheet 3 Column A and locked

It has to be if Sheet 2 Col A contains those words, not exact match

so my end goal is Sheet 3 acts as a dictionary and I'll keep adding words there, and everytime I import a new data in sheet 2 I can just run a macro and remove rows if those words are found in sheet 3.

thank you so much in advance!
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I hope it works:
VBA Code:
Sub test()
  Dim dic As Variant, records As Variant
  With Worksheets("Sheet3")
    dic = Intersect(.UsedRange, .Columns("A"))
  End With
  With Worksheets("Sheet2")
    records = Intersect(.UsedRange, .Columns("A"))
    For i = UBound(records) To 1 Step -1
      For Each itm In dic
        If records(i, 1) Like "*" & itm & "*" Then
          .Rows(i).EntireRow.Delete
        End If
      Next
    Next
  End With
End Sub
 
Upvote 0
I hope it works:
VBA Code:
Sub test()
  Dim dic As Variant, records As Variant
  With Worksheets("Sheet3")
    dic = Intersect(.UsedRange, .Columns("A"))
  End With
  With Worksheets("Sheet2")
    records = Intersect(.UsedRange, .Columns("A"))
    For i = UBound(records) To 1 Step -1
      For Each itm In dic
        If records(i, 1) Like "*" & itm & "*" Then
          .Rows(i).EntireRow.Delete
        End If
      Next
    Next
  End With
End Sub
thanks a lot! it works perfectly.
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,638
Members
452,663
Latest member
MEMEH

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