Delete Rows which contains a specific text in a cell

An Quala

Board Regular
Joined
Mar 21, 2022
Messages
146
Office Version
  1. 2021
Platform
  1. Windows
Hello, can anyone please help me to write a code which delete all the rows if Column AM in sheet "Sponsored Products Campaigns" has text as same as in sheet "Control Panel" Cell S7, so for example it will search the text from Cell S7 Sheet "Control Panel" in the Column AM sheet "Sponsored Products Campaigns" and delete all the rows which have the same text, thank you so much.

Note: Please note that the match is not exact like if cell S7 has "kitchen" so we will need to delete the row with "kitchen tools" as well.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
One way.
VBA Code:
Sub DeleteSpecificText()
    Dim WS1 As Worksheet, WS2 As Worksheet
    Dim FilterRange As Range, DataRange As Range, DeleteRange As Range
    Dim BadText As String
    
    Set WS1 = ThisWorkbook.Worksheets("Control Panel")
    Set WS2 = ThisWorkbook.Worksheets("Sponsored Products Campaigns")
    
    BadText = Trim(WS1.Range("S7").Value)
    
    WS2.AutoFilterMode = False
    Application.ScreenUpdating = False
    
    With WS2
        Set FilterRange = .Range("AM1:AM" & .Range("AM" & .Rows.Count).End(xlUp).Row)    'FilterRange must always include a header row
    End With
    
    With FilterRange
        Set DataRange = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count)
    End With
    
    FilterRange.AutoFilter Field:=1, Criteria1:="*" & BadText & "*"   'Adjust Field as required
    Set DeleteRange = Application.Intersect(FilterRange.SpecialCells(xlCellTypeVisible).EntireRow, DataRange.EntireRow)
    
    WS2.AutoFilterMode = False
    
    If Not DeleteRange Is Nothing Then
        DeleteRange.Delete                            'delete visible rows
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

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