+ Reply to Thread
Results 1 to 15 of 15

Delete rows if older than current month

  1. #1
    Registered User
    Join Date
    12-10-2019
    Location
    Urbana, OH
    MS-Off Ver
    2016
    Posts
    11

    Delete rows if older than current month

    I have a workbook we are using as a schedule. First worksheet is the actual schedule, and once a project is completed, I have a macro that moves it to a "Completed" worksheet and deletes it from the schedule. Every month, we will create a copy and save the "Completed" worksheet as the month of completion. From there, I want to clear the "Completed" worksheet of everything from the previous month (i.e. Everything done in the month of April, I want it deleted so there will only be projects completed in the month of May). Currently this is what I have:

    Please Login or Register  to view this content.
    It somewhat works. The issue comes into play depending on the amount of days within a month. I don't care about the days, just which months they are in. V6 is the start of the range, and V100 should be a cell we never reach in a month. The second issue I am running into is, it usually takes two or more times of running it before it does delete the rows.

  2. #2
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Delete rows if older than current month

    You might kick yourself after seeing this... LOL
    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,381

    Re: Delete rows if older than current month

    Also take the year into consideration. Because if you are in November 2022 and you already have data for January 2023, the data of January 2023 will be deleted too.
    So:

    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,381

    Re: Delete rows if older than current month

    Or use entirerow.delete ofcourse

  5. #5
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Delete rows if older than current month

    Quote Originally Posted by JEC. View Post
    Also take the year into consideration. Because if you are in November 2022 and you already have data for January 2023, the data of January 2023 will be deleted too.
    So:

    Please Login or Register  to view this content.
    Instead of adding all of that in red... why not add something as simple as (below in green):
    Please Login or Register  to view this content.
    I could be off JEC, I know you're a great VBA coder but this just seems like a simpler and more readable solution

  6. #6
    Registered User
    Join Date
    12-10-2019
    Location
    Urbana, OH
    MS-Off Ver
    2016
    Posts
    11

    Re: Delete rows if older than current month

    EDIT: Sorry, I should have entered data into the rows to confirm. JEC, on yours it deletes just the cells in column V. I tried entirerow.delete, but it still only deletes cells in column V and then started leaving #N/A in the cells it deletes and blank ones down to V100.

    I definitely wouldn't say I am kicking myself. lol, I am still quite a layperson to VBA. I tend to read up on examples and modify lines to see if it does what I want. My next task that has been getting stuck is auto email VBA when a cell contains certain text. Outlook tends to get stuck when opening, but that is another mess for the future!

    Thank you to both! Both did what I was looking for, and included something I wouldn't have known till next year. But JEC did win for deleting all rows the first time. I am guessing that is due to using On Error Resume Next. For some reason, any macro I have that deletes rows no longer deletes all the rows the first time. They did last week, but not this week. So I think I need to slide that into my other one and see if that fixes the double click to delete all. The other interesting part is seeing column V flash #N/A when it runs.
    Last edited by Ian_12; 05-25-2022 at 09:53 AM.

  7. #7
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Delete rows if older than current month

    Mine will delete the entire row but in all seriousness, JEC is a better VBA programmer than me lol

  8. #8
    Registered User
    Join Date
    12-10-2019
    Location
    Urbana, OH
    MS-Off Ver
    2016
    Posts
    11

    Re: Delete rows if older than current month

    Yes, yours does delete the whole row but I found a couple issues I can't resolve. Might also just need to close excel and see if it fixes it. So it still won't delete each row the first time. I have to click twice to clear everything. The next thing is, it doesn't want to delete a row if it is from the previous year and a month greater than the current. So example, I put a date of 12/20/2021 and 4/30/2021. It would delete 4/30/2021, but not 12/20/2021.

    EDIT:

    Here is what I have now. This is doing what I expect besides requiring several runs for it to delete all the rows.

    Please Login or Register  to view this content.
    Last edited by Ian_12; 05-25-2022 at 10:53 AM.

  9. #9
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Delete rows if older than current month

    Quote Originally Posted by Ian_12 View Post
    Yes, yours does delete the whole row but I found a couple issues I can't resolve. Might also just need to close excel and see if it fixes it. So it still won't delete each row the first time. I have to click twice to clear everything. Next, it doesn't want to delete a row if it is from the previous year and a month greater than the current. So example, I put a date of 12/20/2021 and 4/30/2021. It would delete 4/30/2021, but not 12/20/2021.
    Right, I didn't think about that. This code (below) will fix that:

    What do you mean by clicking twice? Does a message box pop up?
    This code will now delete rows before the year 2022 & disable all excel pop-ups

    Please Login or Register  to view this content.

  10. #10
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Delete rows if older than current month

    Quote Originally Posted by Ian_12 View Post
    Yes, yours does delete the whole row but I found a couple issues I can't resolve. Might also just need to close excel and see if it fixes it. So it still won't delete each row the first time. I have to click twice to clear everything. The next thing is, it doesn't want to delete a row if it is from the previous year and a month greater than the current. So example, I put a date of 12/20/2021 and 4/30/2021. It would delete 4/30/2021, but not 12/20/2021.

    EDIT:

    Here is what I have now. This is doing what I expect besides requiring several runs for it to delete all the rows.

    Please Login or Register  to view this content.
    Wait.... is your range more than V6->V100?? That's the only reason I can see it requiring to run multiple times.

    Change your range to

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    12-10-2019
    Location
    Urbana, OH
    MS-Off Ver
    2016
    Posts
    11

    Re: Delete rows if older than current month

    By clicking I meant a button I have the macro assigned to. In order to delete all the rows that match the criteria, I have to use it at least twice or even more. But no, there shouldn't ever be any data beyond row 100. Just for some more insight, here is a shot of what I am working on

    Attachment 781693

    So V will be the completed date of the project. That column will dictate which rows need to be deleted.

  12. #12
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Delete rows if older than current month

    Quote Originally Posted by Ian_12 View Post
    By clicking I meant a button I have the macro assigned to. In order to delete all the rows that match the criteria, I have to use it at least twice or even more. But no, there shouldn't ever be any data beyond row 100. Just for some more insight, here is a shot of what I am working on

    Attachment 781693

    So V will be the completed date of the project. That column will dictate which rows need to be deleted.
    See the yellow ribbon on the top of the webpage for attaching files. The picture doesn't come thru when using the button in the quick reply screen.

  13. #13
    Registered User
    Join Date
    12-10-2019
    Location
    Urbana, OH
    MS-Off Ver
    2016
    Posts
    11

    Re: Delete rows if older than current month

    Ah yes, forgot. Wasn't really thinking of posting the workbook. This is just the sheet I was dealing with. I left both macros in it and haven't tweaked them anymore. The dates in column V should be a good range of things to test out.
    Attached Files Attached Files

  14. #14
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,381

    Re: Delete rows if older than current month

    Important thing if you want to delete rows one by one:
    Always go from bottom to top. That's the reason why you have to click twice in this case.

    So here are two options: (first one deletes the specific rows at once)

    Please Login or Register  to view this content.

    Please Login or Register  to view this content.

  15. #15
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    818

    Re: Delete rows if older than current month

    Or:
    Please Login or Register  to view this content.
    Kind regards, Harry.

+ 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] Transfer Data depending on date that is older than from the current month
    By JACK JOUSH in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-14-2021, 08:05 AM
  2. VBA Macro to delete all rows except current month
    By maxi1982 in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 11-07-2019, 07:53 PM
  3. Replies: 1
    Last Post: 10-20-2016, 04:11 AM
  4. Replies: 7
    Last Post: 05-26-2016, 01:49 PM
  5. Delete Rows if Date in Column A is Older Than 6 Years
    By BantamPCI in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-12-2014, 06:13 AM
  6. Delete Rows with dates older than previous Friday
    By DoodlesMama in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-16-2013, 04:51 PM
  7. Deleting rows older than current date and inserting a new row
    By Dan E. in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-14-2005, 11:20 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