+ Reply to Thread
Results 1 to 9 of 9

Delete Rows Which Have Date 7 Days Old

  1. #1
    Registered User
    Join Date
    01-08-2010
    Location
    Suffolk, UK
    MS-Off Ver
    Excel 2003
    Posts
    29

    Question Delete Rows Which Have Date 7 Days Old

    Hi,

    I was wondering if somebody could help me with a little bit of code?

    When the sheet is opened, I would like some code to search down column A, starting from cell "A5" and delete all rows which contain a date which is greater than 7 days ago.

    The dates are in order from the most recent being at the top.

    For example:
    Today is 29/01/2010, I would like all rows which have a cell in column A containing Today-7() to be deleted. 28/01/2010 to stay. 19/01/2010, delete row.


    Hope that makes sense?

    Cheers,

    LSM

    *cross post @ http://www.mrexcel.com/forum/showthread.php?t=444663*
    Last edited by LSM1604; 01-31-2010 at 05:28 PM. Reason: Solved :D

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Delete Rows Which Have Date 7 Days Old

    Hi,

    No need for VBA. Why not just use a helper column with the formula =TODAY()-A5, format it as a number and after copying it down your range filter this column for >7 then delete all the filtered rows.

    (You could of course record a macro to do all that and achieve the same thing)

    HTH
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    01-08-2010
    Location
    Suffolk, UK
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Delete Rows Which Have Date 7 Days Old

    Hi,

    Thanks for the reply, sounds logical. But, exactly how do I do this? I am new to this stuff :S.

    Thanks,

    LSM

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Delete Rows Which Have Date 7 Days Old

    Hi,

    Assuming all your dates are in column A put a column label like 'Date' in A1 with your dates starting in A2, in B2 enter

    Please Login or Register  to view this content.
    and copy this down column B against all your dates.

    Now put your cursor in A1 and from the Menu choose Data>Filter>AutoFilter
    Click the drop down arrow in B1 and select 'Custom' and choose 'is greater than' and in the other drop down enter '7'

    Now select all the filtered rows and delete them.

    HTH

  5. #5
    Registered User
    Join Date
    01-08-2010
    Location
    Suffolk, UK
    MS-Off Ver
    Excel 2003
    Posts
    29

    Talking Re: Delete Rows Which Have Date 7 Days Old

    Hi,

    Thank you for the prompt reply. Between me replying to the previous post and checking back to seeing that you had replied, I managed to work it out with a little VBA:

    Please Login or Register  to view this content.
    Once again, thank you for all your help.

    LSM

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Delete Rows Which Have Date 7 Days Old

    Hi,

    Well done and clearly that will work. However note that generally speaking, loops should only be used for relatively small ranges since there is a time overhead involved every time the VBE has to jump back to the Excel App, and vice versa. You'll find that with large ranges this can start to get time consuming. Always try and use standard Excel App. functionality since this will be quicker. In this case you could still have a macro, but instead of looping down a range just have it perform the Data Filter and delete rows functionality.

    Regards

  7. #7
    Registered User
    Join Date
    01-08-2010
    Location
    Suffolk, UK
    MS-Off Ver
    Excel 2003
    Posts
    29

    Cool Re: Delete Rows Which Have Date 7 Days Old

    Hi,

    Thank you for the advice, I will take the advice on board about the loops. I may have a play with data filters (I will have a search on the net).

    Thanks again,

    LSM

  8. #8
    Registered User
    Join Date
    12-04-2013
    Location
    Pakistan
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Delete Rows Which Have Date 7 Days Old

    Can you please more elaborate this, how did u use this VBA as this is not working on my sheet?

  9. #9
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Delete Rows Which Have Date 7 Days Old

    Hi, and welcome to the forum.
    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too. And since your workbook is going to be different then you will need to provide us with it - in a new thread not here!

+ 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