+ Reply to Thread
Results 1 to 6 of 6

VBA to delete all backups over 5 (qty)

  1. #1
    Forum Contributor
    Join Date
    11-08-2017
    Location
    Murfreesboro, TN
    MS-Off Ver
    MS 365
    Posts
    168

    VBA to delete all backups over 5 (qty)

    Hi.

    I have a macro that backs up a user's custom data in a spreadsheet.

    I would like to add something to the existing macro before it saves to:
    1) check and see how many backups are already in the Backups folder and
    2) ask the user if they would like to delete the oldest if there will be more than 5 copies after this one saves, and
    3) If they say Yes, then deletes the oldest backup file(s).

    Here is the current saving code:
    Please Login or Register  to view this content.
    Thanks!

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,937

    Re: VBA to delete all backups over 5 (qty)

    Below is my code that keeps the newest intFiles2Keep backup files in a subfolder named Backups. It requires a reference to MS Scripting Runtime - it assumes that all the backups in the target folder are backups of the same file. You can modify the code to check the non-date part of the file name if that is not the case. I run this as part of the actual backup routine, so it never needs to look for more than one file - if you haven't cleaned out the folder in a while, it will remove multiple files - just not efficiently

    Use the code like

    CleanUpBackups ThisWorkbook.Path, 5

    You can modify the interaction to ask the questions you want - though most users overestimate the number of backups they need or will ever use.


    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Contributor
    Join Date
    11-08-2017
    Location
    Murfreesboro, TN
    MS-Off Ver
    MS 365
    Posts
    168

    Re: VBA to delete all backups over 5 (qty)

    Thank you for your help. I will give this a try tomorrow and let you know how it goes.

  4. #4
    Forum Contributor
    Join Date
    11-08-2017
    Location
    Murfreesboro, TN
    MS-Off Ver
    MS 365
    Posts
    168

    Re: VBA to delete all backups over 5 (qty)

    Quote Originally Posted by Bernie Deitrick View Post

    Use the code like

    CleanUpBackups ThisWorkbook.Path, 5
    I do not understand where to enter this line in the code.

  5. #5
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,937

    Re: VBA to delete all backups over 5 (qty)

    You can use that anywhere you want, like right after saving the backup: ( I changed ThisWorkbook to wkbTarget since I'm not sure which workbook is which - that should work with your code, without seeing all of your code.)

    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    11-08-2017
    Location
    Murfreesboro, TN
    MS-Off Ver
    MS 365
    Posts
    168

    Re: VBA to delete all backups over 5 (qty)

    Quote Originally Posted by Bernie Deitrick View Post
    You can use that anywhere you want, like right after saving the backup: ( I changed ThisWorkbook to wkbTarget since I'm not sure which workbook is which - that should work with your code, without seeing all of your code.)

    Please Login or Register  to view this content.
    Great! I understand. Thanks so much.

+ 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. Save backups at intervals
    By tone640 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-12-2011, 07:24 AM
  2. Replies: 0
    Last Post: 07-27-2006, 01:05 AM
  3. [SOLVED] Excel backups
    By Allore in forum Excel General
    Replies: 11
    Last Post: 01-26-2006, 01:35 AM
  4. automatic backups
    By bmccormic in forum Excel General
    Replies: 1
    Last Post: 11-14-2005, 02:00 PM
  5. Can I delete .xlk backups and why are they created?
    By dgoranson in forum Excel General
    Replies: 4
    Last Post: 07-11-2005, 07:05 PM
  6. [SOLVED] Backups default
    By Ron Patterson in forum Excel General
    Replies: 5
    Last Post: 06-22-2005, 08:05 PM
  7. RE: How do I do automatic backups in excel xp?
    By Peo Sjoblom in forum Excel General
    Replies: 0
    Last Post: 06-08-2005, 01:05 PM
  8. How do I do automatic backups in excel xp?
    By lettbalt in forum Excel General
    Replies: 0
    Last Post: 06-08-2005, 01:05 PM

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