VBA Delete row if column contains certain text but ignore if a different text is in a different column

tropics123

Board Regular
Joined
May 11, 2016
Messages
85
Hi, looking for some help to take this macro a step further. An illustration below but this is not exact because my sheet will vary with different number of rows daily.

What I'm trying to do: If column E is "0" then delete entire row but if column B has "RK003" then do not delete that row, even if column E has the value "0". My macro can only do the first half of what needs to be done but the second half (in orange font), I don't know how to do. I appreciate your help!

With ActiveSheet
.AutoFilterMode = False
With Range("E1", Range("E" & Rows.Count).End(xlUp))
.AutoFilter 1, "0"
On Error Resume Next
.Offset(1).SpecialCells(12).EntireRow.Delete
End With
.AutoFilterMode = False
End With


End Sub

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]column A[/TD]
[TD]column B[/TD]
[TD]column C[/TD]
[TD]column D[/TD]
[TD]Column E[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]RK001[/TD]
[TD][/TD]
[TD][/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]RK003[/TD]
[TD][/TD]
[TD][/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]RK005[/TD]
[TD][/TD]
[TD][/TD]
[TD]687[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]RK008[/TD]
[TD][/TD]
[TD][/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]RK003[/TD]
[TD][/TD]
[TD][/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]RK003[/TD]
[TD][/TD]
[TD][/TD]
[TD]100[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]RK009[/TD]
[TD][/TD]
[TD][/TD]
[TD]28[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]RK002[/TD]
[TD][/TD]
[TD][/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]RK007[/TD]
[TD][/TD]
[TD][/TD]
[TD]155,000[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try this (assuming row 1 has headers):

Code:
Sub FilterAndDelete()
 Dim LR As Long
 LR = Cells(Rows.Count, "B").End(xlUp).Row
    Range("A1:E" & LR).Select
    Selection.AutoFilter
    ActiveSheet.Range("A1:E" & LR).AutoFilter Field:=5, Criteria1:="0"
    ActiveSheet.Range("A1:E" & LR).AutoFilter Field:=2, Criteria1:="<>RK003"
    ActiveSheet.Range("$A$1:$E$" & LR).Offset(1, 0).SpecialCells _
    (xlCellTypeVisible).EntireRow.Delete
    ActiveSheet.ShowAllData
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,937
Messages
6,175,512
Members
452,650
Latest member
Tinfish

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