VBA to search for values and delete rows

swingline

New Member
Joined
Oct 29, 2019
Messages
15
This macro works great for me for one name but I would like to modify it to allow for additional names to be added.

Code:
Sub Cdelete()
With ActiveSheet
       .AutoFilterMode = False
       With Range("d13", Range("d" & Rows.Count).End(x1Up))
              .AutoFilter 1, "*NAME*"
              On Error Resume Next
                .Offset(1).SpecialCells(12).EntireRow.Delete
       End With
        .AutoFilterMode = False
End With 
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Something like this perhaps.
Code:
Sub Cdelete()
Dim idx As Long
Dim arrNames As Variant

    arrNames = Array("NAME", "NAME1", "NAME2")
    
    For idx = LBound(arrNames) To UBound(arrNames)
    
        With ActiveSheet
            .AutoFilterMode = False
            With Range("d13", Range("d" & Rows.Count).End(x1Up))
                .AutoFilter 1, "*" & arrNames(idx) & "*"
                On Error Resume Next
                .Offset(1).SpecialCells(12).EntireRow.Delete
            End With
            .AutoFilterMode = False
        End With
    
    Next idx
    
End Sub
 
Upvote 0
About how many rows of data do you have?
About how many 'names' in the list to delete?

If both answers are not too big, then here is another approach to try in a copy of your workbook.

If your data set is very large &/or the macros suggested are not super-fast then an alternative faster method can be suggested.

This code assumes that Z1:Z2 are empty & can be used as helper cells. You could use another pair of cells if required.

Rich (BB code):
Sub Del_Rows()
  Dim rCrit As Range
  
  Application.ScreenUpdating = False
  Set rCrit = Range("Z1:Z2")
  rCrit.Cells(2).Formula = "=AGGREGATE(15,6,SEARCH({""Paul"",""Jen"",""Ken""},D14),1)"
  With Range("D13", Range("D" & Rows.Count).End(xlUp))
    .AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=rCrit, Unique:=False
    .Offset(1).EntireRow.Delete
  End With
  rCrit.ClearContents
  If ActiveSheet.FilterMode Then ActiveSheet.ShowAllData
  Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Something like this perhaps.
Code:
Sub Cdelete()
Dim idx As Long
Dim arrNames As Variant

    arrNames = Array("NAME", "NAME1", "NAME2")
    
    For idx = LBound(arrNames) To UBound(arrNames)
    
        With ActiveSheet
            .AutoFilterMode = False
            With Range("d13", Range("d" & Rows.Count).End(x1Up))
                .AutoFilter 1, "*" & arrNames(idx) & "*"
                On Error Resume Next
                .Offset(1).SpecialCells(12).EntireRow.Delete
            End With
            .AutoFilterMode = False
        End With
    
    Next idx
    
End Sub


Thanks for the reply sorry for the delay in testing but I was out of office for a couple days. This causes a Run-time error.

Run-time error '1004'
application-defined or object-defined error
 
Upvote 0
Something like this perhaps.
Code:
Sub Cdelete()
Dim idx As Long
Dim arrNames As Variant

    arrNames = Array("NAME", "NAME1", "NAME2")
   
    For idx = LBound(arrNames) To UBound(arrNames)
   
        With ActiveSheet
            .AutoFilterMode = False
            With Range("d13", Range("d" & Rows.Count).End(x1Up))
                .AutoFilter 1, "*" & arrNames(idx) & "*"
                On Error Resume Next
                .Offset(1).SpecialCells(12).EntireRow.Delete
            End With
            .AutoFilterMode = False
        End With
   
    Next idx
   
End Sub

This morning this seems to have randomly stopped working. It appears to run, but no rows are deleted. I receive no errors, so I'm not sure where to start with troubleshooting. I have tried using multiple computers with no luck. I am using office 2010 pro.
 
Upvote 0
Perhaps try using a different code and see if the same issue happens?
 
Upvote 0

Forum statistics

Threads
1,224,737
Messages
6,180,670
Members
452,993
Latest member
FDARYABEE

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