Delete Entire row if cell does NOT match?

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,382
hello,

the code below is, by far, the easiest and fastest I have ever seen to delete an entire row.

VBA Code:
Sub DeleteRows() 'This macro will delete the entire row if there is a data match anywhere in the range.
   
    With Range("A1:A:500")
        .Replace "John", True, xlWhole ' can also use *John* if searching for any part
        .SpecialCells(xlCellTypeConstants, 4).EntireRow.Delete
    End With
End Sub

what I would like to know is how to tweak the about code to delete the entire row if it does NOT mach the criteria?

example: the above will delete the row if "John" is in Column A
I would like to delete the row if "John" is NOT in Column A

thanks for looking,

Ross
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
How about
VBA Code:
Sub DeleteRows() 'This macro will delete the entire row if there is a data match anywhere in the range.
  
    With Range("A2:A500")
        .Replace "John", "=xxxJohn", xlWhole ' can also use *John* if searching for any part
        .SpecialCells(xlCellTypeConstants).EntireRow.Delete
        .Replace "=xxxJohn", "John", xlWhole
    End With
End Sub
 
Upvote 0
Fluff,

awesome: very nice
i set the string as variable. (see revised code)

there is an issue that arises:
if there is match (no rows to delete)
the code changes the cell value and display up an error.
and idea on how to fix that?

VBA Code:
Sub KeepRows()
 Dim keep As String
  keep = "John"
  
    With Range("A2:A500")
        .Replace keep, "=xxx" & keep, xlWhole
        .SpecialCells(xlCellTypeConstants).EntireRow.Delete
        .Replace "=xxx" & keep, keep, xlWhole
    End With
End Sub

Thanks,
Ross
 
Upvote 0
You can wrap it in a On Error
VBA Code:
Sub KeepRows()
 Dim keep As String
  keep = "John"
  
    With Range("A2:A500")
        .Replace keep, "=xxx" & keep, xlWhole
        On Error Resume Next
        .SpecialCells(xlCellTypeConstants).EntireRow.Delete
        On Error GoTo 0
        .Replace "=xxx" & keep, keep, xlWhole
    End With
End Sub
 
Upvote 0
Would this be a valid alternative?
VBA Code:
Sub Delete_Non_John_by_Fluff() 'This macro will delete the entire row if there is a data match anywhere in the range.
If WorksheetFunction.CountIf(Columns(1), "John") = 0 Then MsgBox "No Johns!": Exit Sub
Application.ScreenUpdating = False
    With Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
        .Replace "John", "=xxxJohn", xlWhole ' can also use *John* if searching for any part
        .SpecialCells(xlCellTypeConstants).EntireRow.Delete
        .Replace "=xxxJohn", "John", xlWhole
    End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,545
Members
449,316
Latest member
sravya

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