Creating a Dated Backup File

Written by Allen Wyatt (last updated October 12, 2019)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


1

Every time Mathew closes a workbook, he'd like to have Excel create a dated backup of that workbook, meaning that it is saved using a filename that includes the date. Thus, if he saves "AnyWorkbookName," it would save not just under that name, but also under the name "AnyWorkbookName [Today's Date & Time]." Mathew is sure this needs to be done with VBA, but he's not sure how to go about it.

There are any number of macros that could be developed to perform this task. Most all of them are variations on a theme (so to speak), so for our purposes a single example should suffice.

The following macro will, just before closing the workbook, save the workbook with a date and time appended to the end of the filename.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim sFileName As String
    Dim sDateTime As String

    With ThisWorkbook
        sDateTime = " (" & Format(Now, "yyyy-mm-dd hhmm") & ").xlsm"
        sFileName = Application.WorksheetFunction.Substitute _
          (.FullName, ".xlsm", sDateTime)
        .SaveCopyAs sFilename
    End With
End Sub

The macro puts together the date and time string into the sDateTime variable. This is then inserted into the workbook's filename by using the SUBSTITUTE worksheet function. (The date/time string is effectively inserted just before the filename extension.) The macro assumes that the workbook is being saved as an XLSM file because it must contain macros—such as the macro to do this saving.

The macro should be placed in the ThisWorkbook module for the workbook. This ensures that it will execute just before the workbook is closed.

There are, as well, third-party add-ins which can perform this task. The following are a few that you may want to check out.

http://www.jkp-ads.com/Download.asp#AutoSafe
http://www.asap-utilities.com

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13195) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Drawing a Table

There are several ways you can create tables in a document, but one of the most unique (and perhaps most fun) is to ...

Discover More

Understanding R1C1 References

Referring to cells is typically done using a letter and a number, which represent the column and row. That's not the only ...

Discover More

Finding a Change in Typeface

When you format a document, you can go so far as to change the font of each character in the document. This may be ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Full Path to Workbook

Need to get the full path of your current workbook into the Clipboard? Excel doesn't make it quite as easy as it used to ...

Discover More

Pulling Filenames into a Worksheet

You can use Excel for all types of data processing. You may want to work with filenames in a worksheet, but the first ...

Discover More

Importing CUSIP Numbers Correctly

It is often necessary to import information from other programs into Excel. Sometimes this can lead to challenges, such ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is nine minus 5?

2019-10-14 10:51:18

Dave Bonin

This would also work if we wrote the file in the .XLSB format instead of .XLSM

Personally, I prefer .XLSB as it makes for a smaller file that seems to load more quickly.

Kudos to Allen for using the "yyyy-mm-dd" format for the date. I often get files whose name has a date code that is unclear, such as 03-04. Is this file from March 4th or April 3rd? Not only does the "yyyy-mm-dd" format remove all ambiguity, it also collates nicely in a directory. (For those under 40, a directory is the original word for folder. This changed around the "Clippy" era.)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.