VBA - Delete row if cell start with "this" or "that"

kparadise

Board Regular
Joined
Aug 13, 2015
Messages
186
Hello,

Looking for VBA code to put in my Module. I have Columns A:D which can have different amount of rows each time i run the code. What I am trying to do is to look in Column D, called "TYPE" and delete and row if the cell in Column D starts with either "Green-" or "Red-".
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I apologize, I meant to say, DELETE the row, if the cell DOES NOT start with "Green-" or "Red-".
 
Upvote 0
Assuming that your data starts on row 1, try this:
Code:
Sub DeleteMacro()

    Dim lRows As Long

'   Filter to only show "Green-" and "Red-" entries in Type column
    Range("A1").CurrentRegion.AutoFilter Field:=4, Criteria1:="=Green-", _
        Operator:=xlOr, Criteria2:="=Red-"

    Application.ScreenUpdating = False
        
'   Delete hidden rows
    For lRows = ActiveSheet.UsedRange.Rows.Count To 1 Step -1
        If Cells(lRows, 1).EntireRow.Hidden = True Then Cells(lRows, 1).EntireRow.Delete
    Next lRows

    Application.ScreenUpdating = True
    
'   Remove filter
    ActiveSheet.UsedRange.AutoFilter
    
End Sub
 
Upvote 0
That works, that you. I changed it to "Green-*", because the cells just need to START with that.
 
Upvote 0
Excellent. Glad you were able to adapt it to work for you.
 
Upvote 0

Forum statistics

Threads
1,213,517
Messages
6,114,089
Members
448,548
Latest member
harryls

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