Not deleting rows that match criteria

fjdurbin

New Member
Joined
Apr 9, 2009
Messages
28
After an extensive search of the web I did not find a satisfying way to delete rows that do not match my criteria. What I ended up doing can be seen below. My solution was to change the color of cells that match the criteria then delete the rows that do not have the cell color. It works but there has to be a better way. Any suggestions?...JD

Code:
Sub ColorRws()
Dim z As Long
Dim x As Long
z = GetLastRowWithData
For x = z To 2 Step -1
If Left(Range("a" & x).Value, 3) = "123" Or Left(Range("a" & x).Value, 3) = "321" Then 'Rows(x & ":" & x).EntireRow.Delete
Range("a" & x).Interior.ColorIndex = 4
End If
Next x

z = GetLastRowWithData
For x = z To 2 Step -1
If Range("a" & x).Interior.ColorIndex <> 4 Then
Rows(x & ":" & x).EntireRow.Delete
End If
Next x
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Not sure if this covers what you're wanting to do, but for something I was working on I used the autofilter in my macro and filtered for rows not equaling FTR. Then selected the rows showing and deleted.

There is probably some cleanup that could be done to improve this, but the code is below...

FYI - the last line of the code is just to reset the autofilter so the list shows all upon completion of the macro.

Code:
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.AutoFilter
        Selection.AutoFilter Field:=17, Criteria1:="<>FTR", Operator:=xlAnd
        Rows("2:2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Delete Shift:=xlUp
        Selection.AutoFilter Field:=17
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

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