+ Reply to Thread
Results 1 to 8 of 8

VBA to delete rows based on date

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Shropshire, England
    MS-Off Ver
    Excel 2007 /10 /13
    Posts
    118

    VBA to delete rows based on date

    Hi all

    I have the following code to delete rows based on dates, but the datea I'm using has over 100,000 records and this is taking forever, is there a fasterway to do this than looping through every line?

        LastRow = Range("A1").End(xlDown).Row
        Do Until ActiveCell.Row = LastRow + 1
        If Range("B" & ActiveCell.Row) < StartDate Or Range("B" & ActiveCell.Row) > EndDate Then
            ActiveCell.EntireRow.Delete
            LastRow = LastRow - 1
        Else
            ActiveCell.Offset(1, 0).Select
        End If
        Loop
    I'd thought about using autofilters, but I've heard they can be very unstable...?

    thanks in advance

  2. #2
    Registered User
    Join Date
    10-10-2015
    Location
    Hoboken, Antwerp, Belgium
    MS-Off Ver
    2010
    Posts
    93

    Re: VBA to delete rows based on date

    Hi,

    It's possible with the find command, p.e:
    Set FoundCell = Range("A1:A100").Find (what:="7/18/1998")
    or
    Set FoundCell = Range("A1:A100").Find _
       (what:=DateValue("July 18, 1998") ,lookin:=xlFormulas)
    Greetings,

    Cheetahke

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA to delete rows based on date

    If you change your mind about autofilters.

    Sub BillDoorz()
    Dim startdate As Date, enddate As Date
    
    With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With
    
    startdate = #9/15/2015#
    enddate = #11/1/2015#
    Range("B1:B" & Range("B" & Rows.Count).End(3).row).AutoFilter 1, ">" & startdate, xlAnd, "<" & enddate
    Range("B2:B" & Range("B" & Rows.Count).End(3).row).SpecialCells(12).EntireRow.Delete
    ActiveSheet.AutoFilterMode = False
    
    With Application
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With
    
    End Sub
    I would suggest testing it by highlighting the rows instead of deleting to see how it performs.

  4. #4
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Shropshire, England
    MS-Off Ver
    Excel 2007 /10 /13
    Posts
    118

    Re: VBA to delete rows based on date

    Hi John

    thanks, but I am having issues with it as I am using UK format dates - dd/mm/yyyy

    Do you know anyway to get around that?

    thanks again

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA to delete rows based on date

    Does this help? Changes to US, deletes rows, changes back.

    Sub BillDoorz()
    Dim startdate As Date, enddate As Date
    
    With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With
    Columns(2).NumberFormat = "m/d/yyyy"
    startdate = #9/15/2015#
    enddate = #11/1/2015#
    Range("B1:B" & Range("B" & Rows.Count).End(3).row).AutoFilter 1, ">" & startdate, xlAnd, "<" & enddate
    Range("B2:B" & Range("B" & Rows.Count).End(3).row).SpecialCells(12).EntireRow.Interior.ColorIndex = 6
    ActiveSheet.AutoFilterMode = False
    Columns(2).NumberFormat = "dd/mm/yyyy;@"
    With Application
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With
    
    End Sub
    Probably better ways though.

  6. #6
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Shropshire, England
    MS-Off Ver
    Excel 2007 /10 /13
    Posts
    118

    Re: VBA to delete rows based on date

    Hi

    still not getting on with that

    VBA really seems to hate UK dates!!

    I'm also changing the line
    Range("B1:B" & Range("B" & Rows.Count).End(3).row).AutoFilter 1, ">" & startdate, xlAnd, "<" & enddate
    to
    Range("B1:B" & Range("B" & Rows.Count).End(3).row).AutoFilter 1, "<" & startdate, xlOr, ">" & enddate
    as I want to delete anything outside of the start and end dates

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA to delete rows based on date

    I think xlAnd should still be your operator in that case as well. Hard to tell from this side for me, but when I tested reversing them as I suggested, it worked.

  8. #8
    Registered User
    Join Date
    10-10-2015
    Location
    Hoboken, Antwerp, Belgium
    MS-Off Ver
    2010
    Posts
    93

    Re: VBA to delete rows based on date

    Hi,

    I've changed the code from John a little bit, can you try this? I'm used to work with Belgian dates, same format as UK dates

    Sub BillDoorz()
    Dim startdate As Date, enddate As Date
    
    With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With
    Columns(2).NumberFormat = "dd/mm/yyyy"
    startdate = datevalue("15/09/2015")
    enddate = datevalue("1/11/2015")
    Range("B1:B" & Range("B" & Rows.Count).End(3).row).AutoFilter 1, ">" & startdate, xlAnd, "<" & enddate
    Range("B2:B" & Range("B" & Rows.Count).End(3).row).SpecialCells(12).EntireRow.Interior.ColorIndex = 6
    ActiveSheet.AutoFilterMode = False
    Columns(2).NumberFormat = "dd/mm/yyyy;@"
    With Application
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With
    
    End Sub

+ 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] Delete rows based on date range
    By hommeg in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-20-2015, 04:59 PM
  2. How to automatically delete rows in Excel based in day of week and date?
    By pr0no in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-04-2012, 03:22 PM
  3. Delete rows based on date
    By randell.graybill in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-09-2009, 01:44 AM
  4. Delete rows based on date
    By mattbrem in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-06-2009, 12:22 PM
  5. Delete Rows based on date range.
    By ge0rge in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-13-2009, 10:52 AM
  6. Macro required to delete rows based on date
    By jalalini in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-13-2008, 03:27 AM
  7. Delete rows based on date range entered?
    By fzrdiva in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-29-2006, 08:52 AM

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