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

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
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
This line seems to be the problem.
With Range("d13", Range("d" & Rows.Count).End(x1Up))
That needs to be a lower case "L" not a digit 1


Did you try the other code too?
 
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,214,376
Messages
6,119,181
Members
448,871
Latest member
hengshankouniuniu

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