Solved

Excel: Macro to Delete Rows Based on a Value

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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…

759 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

17 Experts available now in Live!

Get 1:1 Help Now