DELETE ROWS , IF CONTAINS A SPECIFIC WORD / WORDS

SAQIBN

New Member
Joined
Oct 16, 2021
Messages
20
Office Version
  1. 2013
Platform
  1. Windows
Hi

I have following Code for:

Search from Column C, and delete the If Contains a "Specific word".

but now i want to add more criteria .
like:

if it contains "Roger" or "George" or "Lilly"
then delete those Rows...

Code is as follows:

VBA Code:
Sub test()
With ActiveSheet
    .AutoFilterMode = False
    With Range("d1", Range("d" & Rows.Count).End(xlUp))
        .AutoFilter 1, "*Roger*"
        On Error Resume Next
        .Offset(1).SpecialCells(12).EntireRow.Delete
    End With
    .AutoFilterMode = False
End With
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
How about this

VBA Code:
Sub jec()
 Dim a() As Variant, ar As Variant, it As Variant, XX As String, i As Long, x As Long
 ar = Range("d2", Range("d" & Rows.Count).End(xlUp))
 XX = InputBox("Choose names(comma seperated!)", "Filter")         'like:  roger,george,lilly
 
 For Each it In Split(XX, ",")
   For i = 1 To UBound(ar)
     If InStr(1, ar(i, 1), it, vbTextCompare) Then
        ReDim Preserve a(x)
        a(x) = ar(i, 1)
        x = x + 1
     End If
   Next
 Next
  
 With Range("d1", Range("d" & Rows.Count).End(xlUp))
   .AutoFilter 1, a, 7
   .Offset(1).EntireRow.Delete
   .AutoFilter
 End With
End Sub
 
Upvote 0
Solution
A slightly alternative approach to JEC's (excellent) response, could be:
VBA Code:
Option Explicit
Sub ConvertToZap()
    Dim ws As Worksheet, LR As Long, i As Long, Arr
    Set ws = Sheet1   '<~~ change to suit
    LR = ws.Cells(Rows.Count, 4).End(xlUp).Row
    Arr = ws.Range("D1:D" & LR)
   
    For i = 1 To LR
        If Arr(i, 1) Like "*Roger*" Or _
            Arr(i, 1) Like "*George*" Or _
            Arr(i, 1) Like "*Lilly*" Then
            Arr(i, 1) = "Zap"
        End If
    Next
   
    ws.Range("D1:D" & LR).Value = Arr
   
    With Range("D1:D" & LR)
       .AutoFilter 1, "Zap"
       .Offset(1).EntireRow.Delete
       .AutoFilter
    End With
End Sub
 
Upvote 0
How about this

VBA Code:
Sub jec()
 Dim a() As Variant, ar As Variant, it As Variant, XX As String, i As Long, x As Long
 ar = Range("d2", Range("d" & Rows.Count).End(xlUp))
 XX = InputBox("Choose names(comma seperated!)", "Filter")         'like:  roger,george,lilly
 
 For Each it In Split(XX, ",")
   For i = 1 To UBound(ar)
     If InStr(1, ar(i, 1), it, vbTextCompare) Then
        ReDim Preserve a(x)
        a(x) = ar(i, 1)
        x = x + 1
     End If
   Next
 Next
 
 With Range("d1", Range("d" & Rows.Count).End(xlUp))
   .AutoFilter 1, a, 7
   .Offset(1).EntireRow.Delete
   .AutoFilter
 End With
End Sub
it's Amazing ..... ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ...

Stay Blessed !!!
 
Upvote 0
A small change at the last part, it always deletes empty cells first.

VBA Code:
Sub jec()
 Dim a() As Variant, ar As Variant, it As Variant, XX As String, i As Long, x As Long
 ar = Range("d2", Range("d" & Rows.Count).End(xlUp))
 XX = InputBox("Choose names(comma seperated!)", "Filter")         'like:  roger,george,lilly
 
 For Each it In Split(XX, ",")
   For i = 1 To UBound(ar)
     If InStr(1, ar(i, 1), it, vbTextCompare) Then
        ReDim Preserve a(x)
        a(x) = ar(i, 1)
        x = x + 1
     End If
   Next
 Next
 
 On Error Resume Next
 With Range("d1", Range("d" & Rows.Count).End(xlUp))
   .SpecialCells(xlCellTypeBlanks).Delete
   .AutoFilter 1, a, 7
   .Offset(1).EntireRow.Delete
   .AutoFilter
 End With
End Sub
 
Upvote 0
A small change at the last part, it always deletes empty cells first.

VBA Code:
Sub jec()
 Dim a() As Variant, ar As Variant, it As Variant, XX As String, i As Long, x As Long
 ar = Range("d2", Range("d" & Rows.Count).End(xlUp))
 XX = InputBox("Choose names(comma seperated!)", "Filter")         'like:  roger,george,lilly
 
 For Each it In Split(XX, ",")
   For i = 1 To UBound(ar)
     If InStr(1, ar(i, 1), it, vbTextCompare) Then
        ReDim Preserve a(x)
        a(x) = ar(i, 1)
        x = x + 1
     End If
   Next
 Next
 
 On Error Resume Next
 With Range("d1", Range("d" & Rows.Count).End(xlUp))
   .SpecialCells(xlCellTypeBlanks).Delete
   .AutoFilter 1, a, 7
   .Offset(1).EntireRow.Delete
   .AutoFilter
 End With
End Sub

A small change at the last part, it always deletes empty cells first.

VBA Code:
Sub jec()
 Dim a() As Variant, ar As Variant, it As Variant, XX As String, i As Long, x As Long
 ar = Range("d2", Range("d" & Rows.Count).End(xlUp))
 XX = InputBox("Choose names(comma seperated!)", "Filter")         'like:  roger,george,lilly
 
 For Each it In Split(XX, ",")
   For i = 1 To UBound(ar)
     If InStr(1, ar(i, 1), it, vbTextCompare) Then
        ReDim Preserve a(x)
        a(x) = ar(i, 1)
        x = x + 1
     End If
   Next
 Next
 
 On Error Resume Next
 With Range("d1", Range("d" & Rows.Count).End(xlUp))
   .SpecialCells(xlCellTypeBlanks).Delete
   .AutoFilter 1, a, 7
   .Offset(1).EntireRow.Delete
   .AutoFilter
 End With
End Sub
Dear JEC,
I've done all.
but last thing please,

Some values are duplicating or Triplicating in Column D,
want to keep 1 row, and delete others from duplication....
 
Upvote 0
Try:

VBA Code:
Sub jec()
 Dim a() As Variant, ar As Variant, it As Variant, XX As String, i As Long, x As Long
 Application.ScreenUpdating = False
 ar = Range("d2", Range("d" & Rows.Count).End(xlUp))
 XX = InputBox("Choose names(comma seperated!)", "Filter")         'like:  roger,george,lilly
 
 For Each it In Split(XX, ",")
   For i = 1 To UBound(ar)
     If InStr(1, ar(i, 1), it, vbTextCompare) Then
        ReDim Preserve a(x)
        a(x) = ar(i, 1)
        x = x + 1
     End If
   Next
 Next
 
 On Error Resume Next
 With Range("d1", Range("d" & Rows.Count).End(xlUp))
   .RemoveDuplicates 1, xlYes
   .SpecialCells(xlCellTypeBlanks).Delete
   .AutoFilter 1, a, 7
   .Offset(1).EntireRow.Delete
   .AutoFilter
 End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,941
Members
449,094
Latest member
teemeren

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