Solved

Excel: Macro to Delete Rows Based on a Value

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

742 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