+ Reply to Thread
Results 1 to 12 of 12

Delete Row Based on IF Equation

Hybrid View

  1. #1
    Registered User
    Join Date
    09-21-2010
    Location
    Boston, MA
    MS-Off Ver
    Excel 2003
    Posts
    81

    Delete Row Based on IF Equation

    Hello all, need a little help completing a macro that deletes rows based on an IF equation:

    If the value in the Col marked “End Date” in the header is less than the value in the Col marked “Output Effective Date” then delete that row. Would need to loop through the entire worksheet (several thousand rows). Something like: IF(value in Col “End Date” is LESS THAN value in Col “Output Effective Date”) THAN delete row

    “End Date” and “Output Effective Date” are not always in the same column, which is why I want to use the header (row 1) as the identifying variable.

    The only exception is if "Output Effective Date" is blank. In this case, the row should NOT be deleted.

    Any help is most appreciated – 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: Delete Row Based on IF Equation

    You can actually do this manually with an AUTOFILTER in a matter of seconds.

    1) In an empty column put in a formula to do the data comparison for you row by row. So if:
    ---Column D is End date
    ---Column E is Output Effective Date
    ---Column T is empty, put this in T2 and copy down:

    =IF(AND($E2<>"", $D2<$E2), "Delete", "Keep")

    2) Click on row1
    2) Select Data > Filter > AutoFilter to turn it on
    3) In the column T you added use the drop down and select "Delete"
    4) Highlight all the visible rows and delete them
    5) Turn off the Autofilter the same way you turned it on
    6) Clear the added column (T)
    _________________
    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
    Registered User
    Join Date
    09-21-2010
    Location
    Boston, MA
    MS-Off Ver
    Excel 2003
    Posts
    81

    Re: Delete Row Based on IF Equation

    Thanks JBeaucaire, any way to do this with VBA code? I'm trying to incorporate this function into a larger macro. Thanks!

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

    Re: Delete Row Based on IF Equation

    Of course you have a good sample workbook to design a macro off of, yes?

  5. #5
    Registered User
    Join Date
    09-21-2010
    Location
    Boston, MA
    MS-Off Ver
    Excel 2003
    Posts
    81

    Re: Delete Row Based on IF Equation

    Yes, attached is a sample report. For example, row 6 (highlighted) should be deleted because the End Date comes before the Output Date. You'll also notice that a few Output Date fields are blank. In this case, nothing should happen (that row should not be deleted).

    Thanks so much for your help!
    Attached Files Attached Files

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

    Re: Delete Row Based on IF Equation

    So, based on your sample, EVERY row is deleted except 16, 17 and 18 because all of them have END DATES that are less the OUTPUT EFFECTIVE DATE?

  7. #7
    Registered User
    Join Date
    09-21-2010
    Location
    Boston, MA
    MS-Off Ver
    Excel 2003
    Posts
    81

    Re: Delete Row Based on IF Equation

    Sorry, perhaps I should have formatted the date with 4 digest instead of 2 - those dates are 2042. So, only row 6 should be deleted because the end date comes before the output effective date.

  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: Delete Row Based on IF Equation

    In E2, the date is 12/31/10.
    In C2 the date is 9/28/42.

    E2 is less than C2 to row 2 would be deleted.

    The same is true for every row except the rows where C is blank.

    If this is incorrect you will have to change the data or change the words you're using to describe what to do. You said if "END DATE" is earlier than "OUTPUT EFFECTIVE DATE" then delete the row. That's true for your entire sample.

  9. #9
    Registered User
    Join Date
    09-21-2010
    Location
    Boston, MA
    MS-Off Ver
    Excel 2003
    Posts
    81

    Re: Delete Row Based on IF Equation

    Sorry about that - I see what you mean. Attached is a new workbook. Row 6 should be deleted because the output date (7/1/2010) comes after the End Date (6/2/2010). Also, rows 16, 17, and 18 should not be deleted even though the output date field is blank. Help is very appreciated!
    Attached Files Attached Files

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

    Re: Delete Row Based on IF Equation

    This is basically the same thing I told you above recorded into a macro:
    Sub RemoveRows()
    'Uses a blank column to create an AUTOFILTER set of formulas to delete all unwanted
    'rows all at once.
    Dim LR As Long:     LR = Range("A" & Rows.Count).End(xlUp).Row
    
        Range("M1") = "key"
        Range("M2:M" & LR).FormulaR1C1 = "=IF(AND(RC3>0, RC5 < RC3), ""Delete"", ""Keep"")"
    
        With Range("M1")
            .AutoFilter
            .AutoFilter Field:=1, Criteria1:="Delete"
            Range("M2:M" & LR).Delete xlShiftUp
            .AutoFilter
            .EntireColumn.ClearContents
        End With
    
    End Sub

  11. #11
    Registered User
    Join Date
    09-21-2010
    Location
    Boston, MA
    MS-Off Ver
    Excel 2003
    Posts
    81

    Re: Delete Row Based on IF Equation

    Thanks so much for your help! I'll give this a try and let you know. Appreciate it!

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

    Re: Delete Row Based on IF Equation

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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