Delete entire row with a condition

YasserKhalil

Well-known Member
Joined
Jun 24, 2010
Messages
852
Hi everybody
I want a code that could delete entire row if the cell value doesn't contain specific text say "man"
 
Option Explicit
Sub test()
ActiveSheet.Range("A1", "A1").End(xlDown).AutoFilter Field:=1, Criteria1:="man"
Application.DisplayAlerts = False
ActiveSheet.UsedRange.Offset(1, 0).SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True
ActiveSheet.AutoFilterMode = False
End Sub
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Option Explicit
Sub test()
ActiveSheet.Range("A1", "A1").End(xlDown).AutoFilter Field:=1, Criteria1:="man"
Application.DisplayAlerts = False
ActiveSheet.UsedRange.Offset(1, 0).SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True
ActiveSheet.AutoFilterMode = False
End Sub


Sorry change to

Code:
Option Explicit
Sub test()
ActiveSheet.Range("A1", "A1").End(xlDown).AutoFilter Field:=1, Criteria1:="<>" & "man"
Application.DisplayAlerts = False
ActiveSheet.UsedRange.Offset(1, 0).SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True
ActiveSheet.AutoFilterMode = False
End Sub
 
Upvote 0
I'm sorry
I didn't understand what he means.......

Then you should have asked me to explain not answer incorrectly which then leads to an incorrect solution :confused:

That said, and though taurean has provided a nice solution, here's my tweaked solution:

Code:
Option Explicit
Sub Macro1()

    'http://www.mrexcel.com/forum/showthread.php?t=572619
    
    'Delete any row(s) in A1:A[where ever the last row is] _
    that does not contain the text 'man' within the cell.

    Dim rngCell As Range, _
        rngDeleteRange As Range
        
    Application.ScreenUpdating = False

    For Each rngCell In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
        If InStr(rngCell.Value, "man") = 0 Then
            'Cater for initial setting of 'rngDelRange' range
            If rngDeleteRange Is Nothing Then
                Set rngDeleteRange = Cells(rngCell.Row, rngCell.Column)
            Else
                Set rngDeleteRange = Union(rngDeleteRange, Cells(rngCell.Row, rngCell.Column))
            End If
        End If
    Next rngCell
    
    If Not rngDeleteRange Is Nothing Then
        rngDeleteRange.EntireRow.Delete
    End If
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,947
Latest member
Gerry_F

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