VBA - Delete entire row if criteria is not met

Lalo2DaG

New Member
Joined
Oct 8, 2015
Messages
8
Can someone please help me with VBA code. Here is my real world scenario. I have a report that contains several different statuses. I only need 4 of the many statuses. Ideally, the VBA code would delete rows that does not contain the 4 statuses. In addition, I would also like the code to not delete the first 7 rows at all. The first 7 rows contain miscellaneous information including the headers. Any help with this would be greatly appreciated.

FYI - I was on another thread and it was suggested that I may try my own thread to meet my needs. Here is the link to the thread I was on.

http://www.mrexcel.com/forum/excel-...applications-delete-row-if-cell-contains.html
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
I have to manipulate reports like this all the time and my favorite way is to have a helper column that says basically "=IF(A2=valueToDelete,NA(),A2)" where valueToDelete in your case would be a status you dont' want. You could probably think of some IF/NOT/OR function to return NA() for the ones you don't want. Anyway, after that helper column has errors in place of the values you don't want, you can use it to delete the rows like this:

Code:
Dim r As RangeOn Error Resume Next
Set r = Sheets("mySheet").Range("D:D").Cells.SpecialCells(xlCellTypeFormulas, xlErrors)
On Error GoTo 0
If Not r Is Nothing Then
  r.EntireRow.Delete
End If

Which deletes rows where the value in column D is #N/A. If this isn't helpful, here is a link that may provide you with more information: How to remove rows based on cell value in Excel?
 
Upvote 0
Hi Lalo2DaG,

I agree with svendiamond about using a helper column (I have answered numerous threads about deleting rows using this method). It is an especially good method for large datasets as it doesn't involve looping.

What are the four statuses you want to keep and what column are they in?

Robert
 
Upvote 0
Thanks svendiamond and Trebor76. I haven't used a helper column before, but it does sound ideal for what I'm trying to do. The four statuses I want to keep are "MAIL","SIGN","EDIT", and "SCHD". These status are in the I column.

I should add, that there are over 50 statuses and I only want to keep 4 of them. I guess I'm not sure how I would do the helper column to do this.
 
Last edited:
Upvote 0
I should add, that there are over 50 statuses and I only want to keep 4 of them. I guess I'm not sure how I would do the helper column to do this.

=IF(OR(A1={"MAIL","SIGN","EDIT","SCHD"}),A1,NA())

Basically what your helper column is doing is returning an error if it's not one of those four. Then you could use the code I posted earlier to delete the rows that have an error in that column.
 
Upvote 0
Try this (though initially on a copy of your data as the results cannot be undone if they're not as expected) while on the sheet in question:

Code:
Option Explicit
Sub Macro1()

    Const lngStartRow As Long = 2 'Starting data row number. Change to suit.
    
    Dim lngMyCol As Long, _
        lngMyRow As Long
    Dim xlnCalcMethod As XlCalculation
            
    With Application
        xlnCalcMethod = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With

    lngMyCol = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
    lngMyRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    With Columns(lngMyCol)
        With Range(Cells(lngStartRow, lngMyCol), Cells(lngMyRow, lngMyCol))
            .Formula = "=VLOOKUP(I2,{""MAIL"";""SIGN"";""EDIT"";""SCHD""},1,FALSE)"
            ActiveSheet.Calculate
            .Value = .Value
        End With
        On Error Resume Next 'Turn error reporting off - OK to ignore 'No cells found' message
            .SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
        On Error GoTo 0 'Turn error reporting back on
        .Delete
    End With
    
    With Application
        .Calculation = xlnCalcMethod
        .ScreenUpdating = True
    End With

    MsgBox "All rows from Col. I where the status was not one of the four desired statuses have now been deleted.", vbInformation

End Sub

Regards,

Robert
 
Upvote 0
Thank you! This works great. At first I was having problems with it because it wouldn't work the way I wanted it to, but I then realized I had a column that contained the initials of the employee assigned and if there was no employee assigned it would contain "***". I think the three *'s where throwing the code off, so I deleted the column and it worked like magic. Thanks for the help.
 
Upvote 0
I think the three *'s where throwing the code off, so I deleted the column and it worked like magic.

No, that shouldn't have made a difference. As long as the statuses were in column I (not even case sensitive) all should be good :confused:

As long as it's working that's all that matters I suppose.

Regards,

Robert
 
Upvote 0
Try this:
Code:
Sub Delete_Me()
Application.ScreenUpdating = False
Dim i As Integer
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "I").End(xlUp).Row

    For i = Lastrow To 8 Step -1
        If Cells(i, 9).Value <> "MAIL" And Cells(i, 9).Value <> "SIGN" And Cells(i, 9).Value <> "EDIT" And Cells(i, 9).Value <> "SCHD" Then
            Rows(i).Delete
            End If
    Next
Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,581
Members
449,089
Latest member
Motoracer88

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