Macro to delete rows if a cell in the row = a value

Will85

Active Member
Joined
Apr 26, 2012
Messages
254
Office Version
  1. 365
Platform
  1. Windows
I need a macro that will delete all applicable rows in the spreadsheet if the value in column C from row 5 down = "Yes".

There are breaks/blanks in between my "Yes" values, so it would have to go from the bottom up to determine the last row that could possibly need to be deleted.

I thought I had it, but I ran into an overflow error so I am starting from scratch.

For example, if C5, C52, and C125 = "Yes" then delete rows 5, 52, and 125

Thank you in advance for your help.
 
Last edited:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi Will85,

Sounds like you have an integer variable but your last row is higher than 32,767 which is the maximum value this variable can handle. See how this goes:

Code:
Option Explicit
Option Compare Text 'Allows for non case matching i.e. Yes = yes
Sub Macro1()

    Dim lngLastRow As Long
    Dim lngMyRow As Long
    Dim xlnCalcMethod As XlCalculation
    
    With Application
        .ScreenUpdating = False
        xlnCalcMethod = .Calculation
        .Calculation = xlCalculationManual
    End With

    lngLastRow = Cells(Rows.Count, "C").End(xlUp).Row
    
    For lngMyRow = lngLastRow To 5 Step -1
        If Range("C" & lngMyRow) = "Yes" Then
            Rows(lngMyRow).EntireRow.Delete
        End If
    Next lngMyRow
    
    With Application
        .Calculation = xlnCalcMethod
        .ScreenUpdating = True
    End With

End Sub

Regards,

Robert
 
Upvote 0
Here's another, non-looping, version to try. Test on a copy of your workbook. It assumes you won't have error codes in column C.

Code:
Sub DeleteRows()

    On Error Resume Next
    With Range("C5:C" & Cells(Rows.Count, "C").End(xlUp).Row)
        .Replace "YES", "#N/A", xlWhole, , False, , False, False
        .SpecialCells(xlConstants, xlErrors).EntireRow.Delete
    End With
End Sub
 
Upvote 0
My "Yes" value is via data validation from a drop down list, is that an issue? Do we need a helper column to identify?
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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