+ Reply to Thread
Results 1 to 7 of 7

Delete Rows Based on Date

  1. #1
    Registered User
    Join Date
    11-28-2011
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    73

    Delete Rows Based on Date

    I have read up on this and I am trying to delete rows with date greater than and earlier then yesterday. I have been anything but successful but I have created a bit of code that takes forever to run with absolutely no positive results. Dates are in column A.

    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    For j = 2 To LastRow
    If Cells(j, "A").Value < Date - 1 Then
    Cells(j, "A").EntireRow.Delete
    End If

  2. #2
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Delete Rows Based on Date

    First of all
    Please Login or Register  to view this content.
    gives a value of 1 as xlUp points to the first row.
    But this would make the routine not entering the loop at all as it would be
    Please Login or Register  to view this content.
    On the other hand xlEnd would point to the last row (=1048576!). And yes, that would make the loop run for ages.

    Last but not least:
    the routine starts at cell A2. If that would have a date < yesterday that entire row would be deleted.
    That makes cell A3 become cell A2. In the next run of the loop the routine check cell A3 and so skipping the cell which in the previous run was cell A3 but now is A2.
    Cheers!
    Tsjallie




    --------
    If your problem is solved, pls mark the thread SOLVED (see Thread Tools in the menu above). Thank you!

    If you think design is an expensive waste of time, try doing without ...

  3. #3
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Delete Rows Based on Date

    When deleting rows need to start at the last row and work your way to the top

    Please Login or Register  to view this content.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Delete Rows Based on Date

    Hi joshag

    Filter the Data for dates that are before or after yesterday then delete the Visible Rows...no looping necessary...this can be done with Code...

    Dates.jpg
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  5. #5
    Registered User
    Join Date
    11-28-2011
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    73

    Re: Delete Rows Based on Date

    Thanks to everyone for their help!
    Tsjallie - thank you for pointing out the errors I made!
    Mike7952 - Your piece works as advertised but takes too long (I have ideas to fix this) but may incorporate this into the finished product
    jaslake - I have yet to try your solution but I believe there is value in it. Lunch first, experiment after.

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Delete Rows Based on Date

    Hi joshag

    Need help holler...

  7. #7
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Delete Rows Based on Date

    Using a filter will absolutely be faster then looping.
    Turning your range into a table will also make it more reliable.

+ 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] Vba to select rows based on date and Delete
    By Wither125 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-03-2016, 11:48 AM
  2. VBA to delete rows based on date
    By BillDoor in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-21-2015, 10:28 AM
  3. [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
  4. Delete rows based on date
    By randell.graybill in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-09-2009, 01:44 AM
  5. Delete rows based on date
    By mattbrem in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-06-2009, 12:22 PM
  6. Delete Rows based on date range.
    By ge0rge in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-13-2009, 10:52 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