+ Reply to Thread
Results 1 to 21 of 21

Need a macro to delete rows based on multiple conditions

Hybrid View

  1. #1
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Need a macro to delete rows based on multiple conditions

    Hello!

    I just recently moved into a new job at work and one of my first duties is creating macros. I am woefully inept at this so far and for the most part have been reverse engineering recorded macros to learn.

    My problem I am looking at is that I have an array of six codes in COLUMN B that indicate reversals of payments. Once I see one of these codes in this column I need the macro to check the same row for a value in COLUMN C once it finds that value it needs to move up a row and check to see if the same value is in that row. If all three values are true it needs to delete both columns.

    Column A (DATES)   Column B (CODES)  Column C (PAYMENTS)
    1/1/1990             1(Valid pmt code)                        $200
    1/1/1990             REV(reversal code)                      $200
    1/1/1990             1                                              $200
    1/1/1990             1                                              $523  
    1/1/1990             6(also a reversal code)                $523
    In this example it would delete rows 1,2,4,5 but leave row 3 intact.

    *This is the code I am currently attempting to use.*

    Sub Formatting()
    
        'THIS IS THE CONDITIONAL FORMATTING TO CHECK FOR REVERSALS'
        
        Dim Firstrow As Long
        Dim LastRow As Long
        Dim Lrow As Long
        Dim CalcMode As Long
        Dim ViewMode As Long
        Dim Cell As Range
        Dim Cell1 As String
        Dim Cell2 As String
        Dim Cell3 As String
        
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With
    
        With ActiveSheet
    
            'We select the sheet so we can change the window view
            .Select
    
            ViewMode = ActiveWindow.View
            ActiveWindow.View = xlNormalView
    
            'Turn off Page Breaks, we do this for speed
            .DisplayPageBreaks = False
    
            'Set the first and last row to loop through
            Firstrow = .UsedRange.Cells(1).Row + 1
            LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
            
            'We loop from Lastrow to Firstrow (bottom to top)
            For Lrow = LastRow To Firstrow Step -1
                
                
                'We check the values in the B column'
                
                With .Cells(Lrow, "A")
        
                    If Not IsError(.Value) Then
                    Cell1 = .Cells(Lrow, "A").Value
                    Cell2 = .Cells(Lrow, "B").Value
                    Cell3 = .Cells(Lrow, "C").Value
    
                    If Trim(.Cells(Lrow, "B").Value) = "REV" And .Cells(Lrow - 1, "C").Value = .Cells(Lrow, "C").Value Then
                     Rows(Lrow).EntireRow.Delete
                     Rows(Lrow - 1).EntireRow.Delete
                   
                    
                    End If
    
                    End If
    
                End With
    
            Next Lrow
    
        End With
    Last edited by WorkAccount2; 03-23-2017 at 12:28 PM. Reason: Added missing CODE tags. Please read and follow the Forum Rules, link above in the menu bar. Thanks.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need a macro to delete rows based on multiple conditions

    This is how I would run those tests, read it through and you'll see. The goal here is to delete all the rows at once at the end, so this should be much faster overall.
    Option Explicit
    
    Sub DeleteReversals()
    Dim delRNG As Range, Rw As Long, LR As Long
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
    For Rw = LR To 2 Step -1
        If Range("B" & Rw).Value = 6 Or Range("B" & Rw).Value = "REV" Then
            If Range("C" & Rw).Value = Range("C" & Rw - 1).Value Then
                If delRNG Is Nothing Then
                    Set delRNG = Range("A" & Rw - 1).Resize(2)
                Else
                    Set delRNG = Union(delRNG, Range("A" & Rw - 1).Resize(2))
                End If
            End If
        End If
    Next Rw
    
    If Not delRNG Is Nothing Then delRNG.EntireRow.Delete xlShiftUp
    
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Valued Forum Contributor unit285's Avatar
    Join Date
    10-29-2015
    Location
    TN
    MS-Off Ver
    Office 365
    Posts
    358

    Re: Need a macro to delete rows based on multiple conditions

    Hello,

    Are "REV" and "6" the only codes you need to track? As is the only Reverse Codes?

  4. #4
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    No I have a total of six codes that would be 3 numbers each, so for instance they would be 200,201,202,203,204,REV. Thank you guys for the quick responses. I attempted to modify the code you posted for all six and the range failed. I assume this is because I tried to split each reversal code into it's own line like so.

    If Range("B" & Rw).Value = "REV" Or _
    Range("B" & Rw).Value = 200 Or_
    Range("B" & Rw).Value = 201 Or_
    Range("B" & Rw).Value = 202 Or_
    Range("B" & Rw).Value = 203 Or_
    Range("B" & Rw).Value = 204 Then

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need a macro to delete rows based on multiple conditions

    For that many, I'd switch to this syntax:
    Option Explicit
    
    Sub DeleteReversals()
    Dim delRNG As Range, Rw As Long, LR As Long
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
    For Rw = LR To 2 Step -1
        Select Case Range("B" & Rw).Value
            Case Is = 200, 201, 202, 203, 204, "REV"
                If Range("C" & Rw).Value = Range("C" & Rw - 1).Value Then
                    If delRNG Is Nothing Then
                        Set delRNG = Range("A" & Rw - 1).Resize(2)
                    Else
                        Set delRNG = Union(delRNG, Range("A" & Rw - 1).Resize(2))
                    End If
                End If
        End Select
    Next Rw
    
    If Not delRNG Is Nothing Then delRNG.EntireRow.Delete xlShiftUp
    
    End Sub

  6. #6
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    Using that syntax I tried to change in my actual numbers which are 600-604 and it tells me the range failed. I cannot run option explicit as my previous macro uses an undefined variable in it's code. Sorry to be a pain.

  7. #7
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    Just wondering if something seems off in the code placed below.

    The code is breaking on the second part. It is throwing an error of 'Method range of object _global failed.' for the second range in this line 'If Range("C" & Rw).Value = Range("C" & Rw - 1).Value Then'.

    If there's anything I missed let me know. Thanks so much for the help in advance!

    Sub DeleteReversals()
    Dim delRNG As Range, Rw As Long, LR As Long
    
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
    
    Rw = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
    
    
    For Rw = LR To 2 Step -1
        Select Case Range("B" & Rw).Value
            Case Is = 600, 601, 602, 603, 604, "REV"
                If Range("C" & Rw).Value = Range("C" & Rw - 1).Value Then
                    If delRNG Is Nothing Then
                        Set delRNG = Range("A" & Rw - 1).Resize(2)
                    Else
                        Set delRNG = Union(delRNG, Range("A" & Rw - 1).Resize(2))
                    End If
                End If
        End Select
    Next Rw
    
    
    If Not delRNG Is Nothing Then delRNG.EntireRow.Delete xlShiftUp
    
    
    End Sub

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need a macro to delete rows based on multiple conditions

    When it breaks, and you DEBUG, what is the value of Rw?

  9. #9
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    The value of Rw = 1 when it breaks.

  10. #10
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    Alright I got it to actually work but it doesn't delete both rows like I originally intended. It delete's the row with the reversal code but not it's counterpart.

    To get it to work I had to change
     For Rw = LR to 2 Step  -1
    I changed this to
     For Rw = LR to 4 Step -1

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need a macro to delete rows based on multiple conditions

    The value of RW should never be 1. The loop is running from the last row up to row 2 and stopping.

    What is the value of LR? That should always be higher than 2. If it's not, change the line of code above the loop where the LR variable is getting determined, choose a column other than "A".

  12. #12
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    I got the ranges defined but deleting the rows doesn't work still.

    This is the code I have:

    Sub DeleteReversals()
    Dim delRNG As Range, Rw As Long, LR As Long
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
    Rw = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
    
    For Rw = LR To 3 Step -1
        Select Case Range("B" & Rw).Value
            Case Is = 600, 601, 602, 603, 604, "REV"
                If Range("C" & Rw).Value = Range("C" & Rw - 1).Value Then
                    If delRNG Is Nothing Then
                        Set delRNG = Range("A" & Rw - 1).Resize(2)
                    Else
                        Set delRNG = Union(delRNG, Range("A" & Rw - 1).Resize(2))
                    End If
                End If
        End Select
    Next Rw
    
    If Not delRNG Is Nothing Then delRNG.EntireRow.Delete xlShiftUp
    
    End Sub
    Also, the value of LR is set as 5000, due to my table range.
    Last edited by WorkAccount2; 03-27-2017 at 02:33 PM. Reason: Forgot info.

  13. #13
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need a macro to delete rows based on multiple conditions

    Let's see this macro in a sample workbook not doing the right thing....

  14. #14
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    I'm unfortunately restricted from uploading it seems. But the problem it highlights is this portion of code now.
     delRNG.EntireRow.Delete xlShiftUp
    It deletes out the reversal code row but leaves the other one.

  15. #15
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need a macro to delete rows based on multiple conditions

    Try zipping the file you're trying to upload.

  16. #16
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    Hopefully this works and makes sense.
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    Here is the formatted version with the table.
    Attached Files Attached Files

  18. #18
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    Sub DeleteReversals()
    Dim delRNG As Range
    Dim Rw As Long
    Dim LR As Long
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
    For Rw = LR To 3 Step -1
        Select Case Range("B" & Rw).Value
            Case Is = 600, 601, 602, 603, 604, "REV"
                If Range("C" & Rw).Value = Range("C" & Rw - 1).Value Then
                    If delRNG Is Nothing Then
                        Set delRNG = Range("A" & Rw - 1).Resize(2)
                    Else
                        Set delRNG = Union(delRNG, Range("A" & Rw - 1).Resize(2))
                    End If
                End If
        End Select
    
    Next Rw
    
    If Not delRNG Is Nothing Then delRNG.Rows(Rw).EntireRow.Delete
    If Not delRNG Is Nothing Then delRNG.Rows(Rw - 1).EntireRow.Delete
    
    End Sub
    This code does what I want it to do but it also stops after it hits that first reversal code so I have to run it twice.

  19. #19
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need a macro to delete rows based on multiple conditions

    The presence of an Excel "Table" is gumming up the works. So, we test for that, remove the table if it's there, run the macro, delete the rows, then put the table back:
    Option Explicit
    
    Sub DeleteReversals()
    Dim delRNG As Range
    Dim Rw As Long
    Dim LR As Long
    Dim IsTable As Boolean
    
    If ActiveSheet.ListObjects.Count > 0 Then IsTable = True
    If IsTable Then ActiveSheet.ListObjects(1).Unlist                            ' convert the table back to a range
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
    For Rw = LR To 3 Step -1
        Select Case Range("B" & Rw).Value
            Case Is = 600, 601, 602, 603, 604, "REV"
                If Range("C" & Rw).Value = Range("C" & Rw - 1).Value Then
                    If delRNG Is Nothing Then
                        Set delRNG = Range("A" & Rw - 1).Resize(2)
                    Else
                        Set delRNG = Union(delRNG, Range("A" & Rw - 1).Resize(2))
                    End If
                End If
        End Select
    Next Rw
    
    If Not delRNG Is Nothing Then delRNG.EntireRow.Delete xlShiftUp
    If IsTable Then ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1").CurrentRegion, , xlYes).Name = "Table1"
    
    End Sub

  20. #20
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    This is perfect, thank you so much for all your hard work on this.

  21. #21
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need a macro to delete rows based on multiple conditions

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro to delete rows based on multiple criteria
    By Andrew E Smith in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-12-2014, 12:17 PM
  2. identifying rows to delete based on multiple conditions
    By DannyGIS in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 06-09-2014, 06:29 PM
  3. [SOLVED] Macro to delete multiple rows based on time of day
    By managermonroe in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-12-2013, 03:00 PM
  4. macro - delete rows based on multiple criteria.
    By ChocksterNo1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-05-2013, 10:22 AM
  5. [SOLVED] Macro to Delete Rows based on Specific Conditions
    By stockgoblin42 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-28-2013, 05:29 PM
  6. [SOLVED] Macro required to delete rows with multiple conditions
    By bigband1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-03-2012, 03:53 AM
  7. [SOLVED] VBA code to delete duplicate rows based on multiple conditions
    By pjsween in forum Excel General
    Replies: 5
    Last Post: 06-27-2012, 01:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1