Solved

Excel: Macro to Delete Rows Based on a Value

Posted on 2014-10-23
3
155 Views
Last Modified: 2014-10-26
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

0
Comment
Question by:dabug80
3 Comments
 
LVL 7

Assisted Solution

by:slubek
slubek earned 250 total points
ID: 40401128
Your macro is working for me, but I had to change variable name from 3mthlastrow to threemthlastrow, because compiler said it is not valid identifier (Excel 2010). Maybe that's problem?
If not, set breapoint in line with delete and check if the condition works (I'd prefer checking cells(row,col).text ).
0
 
LVL 33

Accepted Solution

by:
Norie earned 250 total points
ID: 40401220
As well as the variable name problem that code would only work if the sheet '3 Month Letter' was the active sheet and if the code was in a standard module.

This code will work whether or not '3 Month Letter' is active and can be placed in any module, though it should really go in a standard module.
Sub mac_deletefalserows()
    '
    ' mac_deletefalserows Macro

Dim ws As Worksheet
Dim mthlastrow As Long
Dim idx As Long

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


        For idx = mthlastrow To 1 Step -1
            If .Range("O" & idx).Value = 2 Then
                .Range("O" & idx).EntireRow.Delete
            End If
        Next idx
    End With

End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:dabug80
ID: 40405541
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now