+ Reply to Thread
Results 1 to 6 of 6

Delete rows with Start date greater than 30 days

Hybrid View

  1. #1
    Registered User
    Join Date
    09-11-2012
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    81

    Delete rows with Start date greater than 30 days

    Hi

    I am new to VBA programming. I am looking to add a macro that does as below:

    compares the start date if it is greater than 30 days, delete the entire row.

    Please find attached the sample datasheet.

    Thanks
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    11-26-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    159

    Re: Delete rows with Start date greater than 30 days

    Hi try dis code in sample file
    Sub Delt()
    
    Dim r As Long
    r = Range("A" & Rows.Count).End(xlUp).Row
    
    For i = r To 2 Step -1
        If Range("D" & i).Value - Range("C" & i).Value > 30 Then Range("D" & i).EntireRow.Delete
    Next i
    
    End Sub

  3. #3
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Delete rows with Start date greater than 30 days

    option:

    Sub test()
    
    Dim lrow As Long
    
    On Error Resume Next
    
    lrow = Cells(Rows.Count, 1).End(xlUp).Row
    
    With Range("f2:f" & lrow)
        
        Application.ScreenUpdating = 0
        
        .Value = "=IF(D2-C2>30,1,"""")"
        .SpecialCells(xlCellTypeFormulas, xlNumbers).EntireRow.Delete
        .Clear
        
        Application.ScreenUpdating = 1
        
    End With
    
    End Sub

  4. #4
    Registered User
    Join Date
    09-11-2012
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    81

    Re: Delete rows with Start date greater than 30 days

    What are D2 and C2 here???? IF this is calculation the diffrence between start and finish dates, thats not what I wanted. I wanted to see if the start date which is column C is older than 30 days then I wanted to delete the entire row.

    Please advice, thanks

  5. #5
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Delete rows with Start date greater than 30 days

    ok, so I assume we compare today's date. Then it could be done this way:

    Sub test()
    
    Application.ScreenUpdating = 0
    
    With ActiveSheet.UsedRange
    
        .AutoFilter 3, "<" & CLng(DateAdd("d", -30, Date))
        If Cells(Rows.Count, 3).End(xlUp).Row > 1 Then .Offset(1).EntireRow.Delete
        .AutoFilter
        
    End With
    
    Application.ScreenUpdating = 1
    
    End Sub
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    09-11-2012
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    81

    Re: Delete rows with Start date greater than 30 days

    THanks very much, its working great

+ 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