Link to home
Start Free TrialLog in
Avatar of dabug80
dabug80

asked on

Excel: Macro to Delete Rows Based on a Value

Hello,

I have the following formula below. I would like to delete every row that has a 2 in column O. Currently I run the macro without errors, but nothing happens.

Thanks

Sub mac_deletefalserows()
'
' mac_deletefalserows Macro

Dim 3mthlastrow As Long
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("3 Month Letter")
    3mthlastrow = Range("A" & Rows.Count).End(xlUp).Row

    Sheets("3 Month Letter").Select
    
For Idx = 3mthlastrow To 1 Step -1
    If Range("O" & Idx).Value = 2 Then
        Range("O" & Idx).EntireRow.Delete
    End If
Next
End Sub

Open in new window

SOLUTION
Avatar of slubek
slubek
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dabug80
dabug80

ASKER

Hi. I've split the points. slubek was correct - I incorrectly named my variable. When I fixed that it worked.

Points awarded to imnorie for building on my solution and making it better, so it can work across all active worksheets.