+ Reply to Thread
Results 1 to 27 of 27

Delete rows after a certain time period has passed

  1. #1
    Forum Contributor
    Join Date
    02-20-2017
    Location
    UK
    MS-Off Ver
    2010
    Posts
    133

    Delete rows after a certain time period has passed

    I would like rows to be deleted when a certain time frame is reached.
    I would like the rows that are yet to meet this time period to be shifted up when the above rows are deleted.

    I have found the following code:

    Please Login or Register  to view this content.
    Can it be improved in any way? I would like this to run automatically when the workbook is opened, rather than having to click run each time - is this possible?

    Thanks

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Delete rows after a certain time period has passed

    To have your macro initiate upon opening the workbook, place this into ThisWorkbook module:

    Please Login or Register  to view this content.
    That way, when you workbook opens, it will 'call' the macro delete_old and run it.

    I haven't tested your macro delete_old. Are you having any issues with it ?

  3. #3
    Forum Contributor
    Join Date
    02-20-2017
    Location
    UK
    MS-Off Ver
    2010
    Posts
    133

    Re: Delete rows after a certain time period has passed

    Quote Originally Posted by Logit View Post
    To have your macro initiate upon opening the workbook, place this into ThisWorkbook module:

    Please Login or Register  to view this content.
    That way, when you workbook opens, it will 'call' the macro delete_old and run it.

    I haven't tested your macro delete_old. Are you having any issues with it ?
    Thanks for replying.

    I already have other code in ThisWorkbook. Do I just paste the above code below?

    Yes I think the macro is working OK, would just like to test from opening the file to confirm.

  4. #4
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Delete rows after a certain time period has passed

    If you already have this sub in ThisWorkbook:

    Please Login or Register  to view this content.
    Then paste this line of code in that sub:

    Please Login or Register  to view this content.
    If this doesn't already exist :

    Please Login or Register  to view this content.
    Then paste this entire macro in ThisWorkbook:

    Please Login or Register  to view this content.
    You can test if the macro is being initiated when the workbook opens by inserting this:

    Please Login or Register  to view this content.
    here, in your original macro :

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    02-20-2017
    Location
    UK
    MS-Off Ver
    2010
    Posts
    133

    Re: Delete rows after a certain time period has passed

    Thanks for your help.

    I get: Compile error:
    Sub or Function not defined when I place "delete_old" in between the:
    Private Sub Workbook_Open()

    End Sub

    Can it go anywhere in here, obviously not amongst the other code.. but I tried to place it the line above End Sub and got the above error?
    Last edited by AliGW; 02-22-2017 at 02:54 PM. Reason: Unnecessary quotation removed.

  6. #6
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Delete rows after a certain time period has passed

    Post your code for review. If the workbook is not too large, having a copy posted here would be best. Remove any confidential info.

  7. #7
    Forum Contributor
    Join Date
    02-20-2017
    Location
    UK
    MS-Off Ver
    2010
    Posts
    133

    Re: Delete rows after a certain time period has passed

    Quote Originally Posted by Logit View Post
    Post your code for review. If the workbook is not too large, having a copy posted here would be best. Remove any confidential info.
    Here's my code:

    Please Login or Register  to view this content.
    If nothing obvious is wrong above then I can upload the file.

  8. #8
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Delete rows after a certain time period has passed

    Try this :

    Paste this macro into a routine module (you may have already had it there to begin with ?):

    Please Login or Register  to view this content.
    Then in ThisWorkbook, paste this macro :

    Please Login or Register  to view this content.

    Now, if you want to test that the Workbook_Open macro is working (calling the delete_old macro) paste this line
    at the top of the delete_old macro:

    Please Login or Register  to view this content.

    Save the workbook, close it, then open it again. The msgbox should show.

  9. #9
    Forum Contributor
    Join Date
    02-20-2017
    Location
    UK
    MS-Off Ver
    2010
    Posts
    133

    Re: Delete rows after a certain time period has passed

    No longer getting the compile error but I do not get a msgbox

    I did not have the code in a module before, I instead had it in the sheet I wanted it to apply to... Was this incorrect? Otherwise how do I state which sheet I want the code to apply to?

    Sorry, I am new to vba!

    Just to confirm, in ThisWorkbook I am just pasting the delete_old in between the existing code, like this?:

    Please Login or Register  to view this content.
    Thanks
    Last edited by AliGW; 02-22-2017 at 02:55 PM. Reason: Unnecessary quotation removed.

  10. #10
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Delete rows after a certain time period has passed

    No need to paste the previous post into your reply, it takes up too much space on the servers.

    Don't use REPLY WITH QUOTE.

    See the attached project.
    Attached Files Attached Files

  11. #11
    Forum Contributor
    Join Date
    02-20-2017
    Location
    UK
    MS-Off Ver
    2010
    Posts
    133

    Re: Delete rows after a certain time period has passed

    Hi - thanks. The delete_old() code within Module 1 is working. When I press play the message box appears. However, I still can't put delete_old within ThisWorkbook.

    My file has a load of other code within ThisWorkbook... so where do I paste the delete_old?
    I already have other code between:
    Please Login or Register  to view this content.
    Also - sorry, how does it know which sheet to apply this delete_old code to when it is being stored within Module1? Does it not need to be stored in the code of the sheet it's applied to?
    Last edited by JRC1; 02-22-2017 at 05:03 AM.

  12. #12
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Delete rows after a certain time period has passed

    You will paste delete_old

    anywhere in this sub:

    Private Sub Workbook_Open()

    End Sub


    Excel (VBA) will search through the entire workbook project to find "delete_old". Once located it will run it.

    In your Post #9, placing the call to the macro delete_old, where you put it, is fine. Some folks include the word ' Call '
    prior to the macro name. Its simply another command telling Excel "Hey ... take notice of this line of code and execute it.
    I want you to go look for this macro and run it."

    Call delete_old

    If you don't include the word Call, Excel still knows it has to go look for the macro and run it.

  13. #13
    Forum Contributor
    Join Date
    02-20-2017
    Location
    UK
    MS-Off Ver
    2010
    Posts
    133

    Re: Delete rows after a certain time period has passed

    But I must need to state the sheet that I want delete_old to apply to somewhere? Otherwise it may just run on Sheet1 and I don't want it to start deleting rows on Sheet 1?

    Either way, I can't get it to work. If I click somewhere in the delete_old code into Module1 and press play, it works and I get the message box "This macro is working". If I then paste "Call delete_old" in ThisWorkbook, save the spreadsheet and re-open, I do not automatically get the "This macro is working" message, and I do not know why!

    Shall I attach my file here so you can have a look? thanks

  14. #14
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Delete rows after a certain time period has passed

    Please attach.

  15. #15
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,777

    Re: Delete rows after a certain time period has passed

    JRC1 - Please don't quote whole posts - it's just clutter. If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding

    For normal conversational replies, try using the QUICK REPLY box below.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  16. #16
    Forum Contributor
    Join Date
    02-20-2017
    Location
    UK
    MS-Off Ver
    2010
    Posts
    133

    Re: Delete rows after a certain time period has passed

    File attached.

    Please note this is a version for this forum. I have a master version with confidential info that I will need to paste the code into - thanks
    Last edited by JRC1; 02-23-2017 at 06:20 PM.

  17. #17
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Delete rows after a certain time period has passed

    Ok ... I changed the location of the call to macro delete_old.

    It now displays when the workbook is opened and executes the macro as desired. (I believe that is what you wanted ?).

    #1 - Please explain the use of the timer code located in the Workbook_Open module. #2 - When should the message box appear to notify the user of the 5 minute deadline ?
    #3 - Are you wanting to autoclose the workbook ? An answer to all three questions (I know they seem the same, but I have an old mind ) is helpful.

    Please Login or Register  to view this content.

  18. #18
    Forum Contributor
    Join Date
    02-20-2017
    Location
    UK
    MS-Off Ver
    2010
    Posts
    133

    Re: Delete rows after a certain time period has passed

    Logit. Thank you for the above code - the delete_old now appears to be working as the "this macro is working" message box now appears when I open the file.

    However, my timer code doesn't appear to be working as it did before (not sure if you moved some bits around or altered it?) - when I open the file my timer message box is stating "This file has been open for 0 minutes. You have 5 minutes to save before Excel closes." But my TimeInMinutes is set to 15, so it should only be displaying this message box after 10 mins with the 5 min warning, not straight away.

    Furthermore, in answer to your questions:
    1) (It's not my code so I'll do my best to answer).... the timer code has been implemented to auto close the workbook after a certain amount of time, to prevent a user from editing and then leaving the workbook open for long periods (due to inactivity or just due to taking too long).
    2) When set to 15, the message box should appear after 10 mins, saying this file has been open for 10 mins, you have 5 mins to save before Excel closes".
    3) Yes, I wish for the workbook to auto close after 15 mins, preferably save and close, rather than just close - not sure if this can be added in?

    If you could help/advise that would be great. The one issue I was facing with the timer code was that when the workbook was opened, the code would auto run and in doing so seemed to lock down the task bars and any menu buttons, etc. It was suggested to me that this may be due to the loop, but I wasn't sure how to amend the code to prevent this whilst keeping its functionality - I need it to auto run when the workbook is opened, but not to lock down the menu's and task bars etc.

    Many thanks

  19. #19
    Forum Contributor
    Join Date
    02-20-2017
    Location
    UK
    MS-Off Ver
    2010
    Posts
    133

    Re: Delete rows after a certain time period has passed

    I also still have to ask regarding the delete_old code, how it knows to look for data in the "Archive" tab, considering this isn't specified anywhere and the code has been placed into Module1, rather than specific sheet code... thanks!

  20. #20
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Delete rows after a certain time period has passed

    Here is the revamped project.

    Look in ThisWorkbook module and review the last few lines of code :

    Please Login or Register  to view this content.

    Re: your question 'how does it know to look in the Archive tab ?

    Right click on the Raw Data tab / View Code.

    These lines of code are referencing the Archive tab:

    Please Login or Register  to view this content.
    Basically, what the red colored line says is : Copy the stuff from the active sheet and these cells
    Please Login or Register  to view this content.
    and paste them over here in the Archive sheet :
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Logit; 02-28-2017 at 12:04 PM.

  21. #21
    Forum Contributor
    Join Date
    02-20-2017
    Location
    UK
    MS-Off Ver
    2010
    Posts
    133

    Re: Delete rows after a certain time period has passed

    I understand the above part of your post regarding copying rows from Raw Data to the Archive tab, that wasn't what I was referring to.

    I'm referring to the delete_old code, which is supposed to delete data from the Archive tab after a set period of time... this has nothing to do with the copying from Raw Data to Archive that you've mentioned above??? - I'm unsure how delete_old knows to delete rows from Archive...


    Anyway, I am still receiving the message "This file has been open for 0 minutes. You have 5 minutes to save before Excel closes" as soon as I open the file... any idea why? I've used the code from your latest post above.

    Thanks

  22. #22
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Delete rows after a certain time period has passed

    in ThisWorkbook, the macro delete_old is 'called' here :

    Please Login or Register  to view this content.
    That tells Excel "go look through the workbook and find this macro "delete_old".

    It locates the macro in Module 1 and knows what to look for and delete in this portion of the code :

    Please Login or Register  to view this content.
    I am not seeing the Warning Message here when I open the file. ???

  23. #23
    Forum Contributor
    Join Date
    02-20-2017
    Location
    UK
    MS-Off Ver
    2010
    Posts
    133

    Re: Delete rows after a certain time period has passed

    Ignore my post about the warning message - that was my mistake, I have sorted it.

    My question is, the big part of code you have put in red above, how does it know that the A2 mentioned is the A2 in Archive?? I don't see it relating to the Archive tab above unless I am completely mistaken and in which case I apologise.

  24. #24
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Delete rows after a certain time period has passed

    I see what you are referring to now.

    Copy and paste this macro - to replace the existing macro 'delete_old' :

    Please Login or Register  to view this content.
    Now, any entry in the ARCHIVE sheet that is older than 30 days will be deleted when the workbook is opened.

  25. #25
    Forum Contributor
    Join Date
    02-20-2017
    Location
    UK
    MS-Off Ver
    2010
    Posts
    133

    Re: Delete rows after a certain time period has passed

    Perfect, works as expected as is just what I'm after. Thanks for your help and for sticking with this.

  26. #26
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Delete rows after a certain time period has passed

    Glad to help. Cheers

  27. #27
    Forum Contributor
    Join Date
    02-20-2017
    Location
    UK
    MS-Off Ver
    2010
    Posts
    133

    Re: Delete rows after a certain time period has passed

    Just one point which does seem to be an issue... the functionality to access toolbars/menu buttons etc still seems to be disabled or over-ran by something in the code when the macro is autorun on startup. Is this something that can be altered?

+ 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. Hilighting cells when a period of time has passed
    By Davycc in forum Excel General
    Replies: 1
    Last Post: 08-20-2014, 10:26 PM
  2. Replies: 8
    Last Post: 06-20-2014, 02:40 AM
  3. [SOLVED] Complicated:Calculate time period based on no. of units sold + fluctuations within period
    By omaha.crab in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-06-2012, 03:04 AM
  4. delete rows outside of 24hr period
    By dmic23 in forum Excel General
    Replies: 1
    Last Post: 07-14-2012, 07:25 PM
  5. Replies: 1
    Last Post: 08-18-2011, 11:36 AM
  6. Replies: 0
    Last Post: 06-28-2006, 04:45 PM
  7. Change colour once a certain time period passed
    By Dianne in forum Excel General
    Replies: 1
    Last Post: 01-10-2006, 02:40 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