Written by Allen Wyatt (last updated October 8, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021
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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13195) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and 2021.
Best-Selling VBA Tutorial for Beginners Take your Excel knowledge to the next level. With a little background in VBA programming, you can go well beyond basic spreadsheets and functions. Use macros to reduce errors, save time, and integrate with other Microsoft applications. Fully updated for the latest version of Office 365. Check out Microsoft 365 Excel VBA Programming For Dummies today!
When dealing with files containing comma-separated values, you want to make sure that what gets imported into Excel ...
Discover MoreExcel provides different file formats you can use to export your worksheet information. One such file format is CSV, or ...
Discover MoreIf you try to open a second workbook and you see an error message, it could be because of the way you are opening the ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.)
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments