?
Solved

Excel: Macro to Delete Rows Based on a Value

Posted on 2014-10-23
3
Medium Priority
?
168 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 7

Assisted Solution

by:slubek
slubek earned 1000 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 34

Accepted Solution

by:
Norie earned 1000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

765 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