Solved

Excel: Macro to Delete Rows Based on a Value

Posted on 2014-10-23
3
157 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

776 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