+ Reply to Thread
Results 1 to 12 of 12

Need to modify macro to also create backup of original file

  1. #1
    Registered User
    Join Date
    03-29-2014
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    73

    Need to modify macro to also create backup of original file

    Hi all, I need to modify the macro bellow to also create a backup copy of the original workbook,

    1- Once the macro is run it will save the current workbook (that is done by the code already) and than close it.
    2- Create a new file with same file name but add today's date, (these files will be kept in same folder)
    3- After 3 backups override the first one, than the second and so on...!

    Can this be done in VBA?

    NOTE: Current Macro saves and closes the workbook but it leaves open excel application with all commands ghosted, I would like it to close workbook and excel also......


    Please Login or Register  to view this content.
    Thank you all in advance

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Re: Need to modify macro to also create backup of original file

    Good morning tfilipe


    The snippet below needs to go after your ThisWorkbook.Save line, and you don't really need your ...Close line, as the code will shut Excel down.
    Please Login or Register  to view this content.
    As for only keeping a rolling 3-copy-backup, that is not as easy as it sounds. What else is in this directory?
    It would make life miles easier if the only thing in this directory was your actual live file and the backups. Nothing else. Then all you need to do is set up your three backups manually, then get the code to delete the oldest file in there every time it is run.

    HTH

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  3. #3
    Registered User
    Join Date
    03-29-2014
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    73

    Re: Need to modify macro to also create backup of original file

    Thank you dominicb,

    I will give that a go,

    U got me a bit confused with "Then all you need to do is set up your three backups manually, then get the code to delete the oldest file in there every time it is run." I am not following your line of thought.

    Cheers

  4. #4
    Registered User
    Join Date
    03-29-2014
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    73

    Re: Need to modify macro to also create backup of original file

    "It would make life miles easier if the only thing in this directory was your actual live file and the backups. Nothing else."

    That is exactly what it is there, the original file and the backups.

    This file is stored in a network drive and 3 dif users access it via shortcuts from their desktops, Re: the following line "fname = "C:\Users\dominic\Documents\" how would we code this to save to original file location? without messing about file path.....

    Cheers

  5. #5
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Re: Need to modify macro to also create backup of original file

    Hi tfilipe

    Replace the line beginning fname with this :
    Please Login or Register  to view this content.
    The code at the bottom should hunt out and delete the oldest file in the directory that your workbook sits in.

    The bit that you didn't follow : You will need to start with three backup files in this directory. The code will always delete the oldest file. If you don't start with three backup files, then the current one will always be deleted, and you will end up with no backup files. If you have three backup files to start with, your macro will create a fourth, my code will delete the oldest, and you are left with the most recent three. See?

    This code will delete the file - it won't be put into a recycle bin. It will be gone. Do not test this on your live files until you are happy that it works as you expect.
    Please Login or Register  to view this content.
    At the moment, the "Kill" command at the bottom is commented out, so the code will tell you what the oldest file is. To make it live, uncomment the "Kill" line and remove the "MsgBox" line.

    HTH

    DominicB

  6. #6
    Registered User
    Join Date
    03-29-2014
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    73

    Re: Need to modify macro to also create backup of original file

    Hi dominicb,

    Thanks for the help so far,

    I created a test folder and placed the original file as well as 3 others, "filename 11-aug-18" filename 12-aug-18" and filename 13-aug-18" hoping that the sub would created a new one dated today and delete the "filename 11-aug-18" it's not happening...!

    The Sub OldestFile(), do i need to call this sub anywhere?

    Bellow the 2 Sub as I have have them in the workbook

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

  7. #7
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Re: Need to modify macro to also create backup of original file

    Hi tfilipe

    Can you post the workbook you're using?
    You can delete any data in it, I don't need any of that - I just want to see the macros in situ and see how they're being called, and test them within the file.

    HTH

    DominicB

  8. #8
    Registered User
    Join Date
    03-29-2014
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    73

    Re: Need to modify macro to also create backup of original file

    Hi dominicb,

    Here you go, file attached
    Attached Files Attached Files

  9. #9
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Re: Need to modify macro to also create backup of original file

    Hi tfilipe

    OK. Replace the two macros you have above, with this one.
    I've combined them both, tidied things up a bit and tested it and it seems to be working fine.

    I have left the MsgBox line in there, but commented out. If you want to test the code first, comment out the "Kill" line, and activate the "MsgBox" line.

    Please Login or Register  to view this content.
    HTH

    DominicB

  10. #10
    Registered User
    Join Date
    03-29-2014
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    73

    Re: Need to modify macro to also create backup of original file

    Hi dominicb, thank you so much for your help... excellent work!!!!

    I tested and it does work, well to good actually let me explain;

    This workbook will be open and close more than once each day so excel will detect the file with today's date on it and asks to replace! and the macro goes and deletes the oldest file in the directory, since this can potentially happen several times a day it will end up deleting all the files just leaving the most recent.... no backups! I tested and it even deleted the original file

    I think a way to overcome this would be to add timestamp to the filename as well "hh:mm:ss" this way the likelihood of two files being saved with the same name "to the second" are very slim or not at all..... I don't want to abuse your kindness but would you please help out altering the macro?

    Thank you in advance

    Cheers
    Tony

  11. #11
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Re: Need to modify macro to also create backup of original file

    Hi tfilipe

    I've reworked the code a little to timestamp each filename with the time. I can't use "hh:mm:ss" as you cannot use the ":" character in filenames. I have gone for "hh-mm-ss") instead. It looks a bit strange but should do the trick.

    I've also reversed the order the macro works in : the code now does the backup first and then saves the master file. This way the main file can never be the oldest file - don't know why i didn't pick up on that before.

    As before, when you start using this, make sure that the folder contains your main file + the 3 backups. If your file works correctly, going forward there should never be more or less than 4 files in the folder.
    Please Login or Register  to view this content.
    Note that the "Kill" line is currently commented out.

    HTH

    DominicB

  12. #12
    Registered User
    Join Date
    03-29-2014
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    73

    Re: Need to modify macro to also create backup of original file

    Super...! It works like a swiss watch

    Thank you dominicb

+ 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] VBA to create dated backup file
    By phbryan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-30-2015, 12:54 PM
  2. Create backup copy of excel file in VB
    By t3623gl in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-06-2012, 03:57 PM
  3. Replies: 6
    Last Post: 03-07-2012, 04:56 PM
  4. Create Backup File Within A Macro
    By nebb in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-10-2007, 09:41 AM
  5. [SOLVED] Backup:always create a backup file in the save options
    By Alek in forum Excel General
    Replies: 1
    Last Post: 01-07-2006, 09:25 PM
  6. [SOLVED] How do I Create backup of excel file in other folder
    By khalid in forum Excel General
    Replies: 1
    Last Post: 05-24-2005, 06:31 AM
  7. [SOLVED] How do I Create backup of excel file in other folder
    By khalid in forum Excel General
    Replies: 1
    Last Post: 05-24-2005, 06:06 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