+ Reply to Thread
Results 1 to 5 of 5

Macro to retain last 2 months

  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Macro to retain last 2 months

    I have dates in Col E. I have tried to write code to retain the last two months and delete all the other dates.

    My dates are in format dd/mm/yyyy

    In this example I want to Retain all the dates containing June 2023 and 2023 and delete the rest from row 2 onwards


    I have highlighted the dates to retain in yellow

    It would be appreciated if someone could kindly amend my code

    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: Macro to retain last 2 months

    Try the following.
    You basically need to change the date variable to a long in the filter criteria.
    I also changed it so that endDate was the max value rather than the last position.

    Also, since you're deleting the visible cells, you may need to filter for the dates you don't want.

    Please Login or Register  to view this content.
    Last edited by ByteMarks; 07-20-2023 at 07:40 AM.

  3. #3
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Re: Macro to retain last 2 months

    Thanks for the help. I need to retain the month and years for the last 2 months being June 2023 and May 2023 in this example. The last month is June 2023 and the Prior month is May 2023, so these must be retained

    The items in Blue must be deleted

    Please test & amend
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: Macro to retain last 2 months

    Please try this.

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Re: Macro to retain last 2 months

    Many thanks for the help Bytemarks


    Your code works 100%

+ 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] macro to retain certain items
    By Howardc1001 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 12-23-2021, 09:02 AM
  2. [SOLVED] macro to retain Certain Data
    By Howardc1001 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-26-2021, 01:09 PM
  3. [SOLVED] Macro to retain data in Col E
    By Howardc1001 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-23-2021, 01:08 PM
  4. Retain top 3 rows with months and years independent of below.
    By David Chapman in forum Excel General
    Replies: 1
    Last Post: 07-10-2020, 07:03 PM
  5. [SOLVED] macro to retain certain Data
    By flupsie in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-05-2018, 03:49 AM
  6. Need Macro to get data for last 3 months, 6 months, 12 months etc
    By idv786 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-15-2016, 09:13 AM
  7. Retain the Value of a Variable in Macro
    By blueperfume300 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-07-2007, 04:19 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