VBA macros · Workbooks and files
VBA macro to save a backup copy with a timestamp in Excel
Save a dated copy of the active workbook in a Backups folder with one click, without changing the file you are working on.
AutoRecover and version history help, but nothing beats a backup you made on purpose before a big change. This macro saves a copy of the active workbook into a Backups folder next to it, with the date and time in the file name, for example Budget_2026-12-03_14-05-30.xlsm. Your open workbook keeps its own name and stays open.
Option Explicit
Sub SaveBackupCopy()
Dim wb As Workbook
Dim sep As String, folder As String
Dim baseName As String, ext As String, target As String
Dim dotPos As Long
Set wb = ActiveWorkbook
If Len(wb.Path) = 0 Then
MsgBox "Save the workbook once first.", vbExclamation
Exit Sub
End If
sep = Application.PathSeparator
folder = wb.Path & sep & "Backups"
If Len(Dir(folder, vbDirectory)) = 0 Then MkDir folder
' Split "Budget.xlsm" into "Budget" and ".xlsm"
dotPos = InStrRev(wb.Name, ".")
If dotPos > 0 Then
baseName = Left$(wb.Name, dotPos - 1)
ext = Mid$(wb.Name, dotPos)
Else
baseName = wb.Name
ext = ""
End If
target = folder & sep & baseName & "_" & Format(Now, "yyyy-mm-dd_hh-nn-ss") & ext
On Error GoTo Fail
wb.SaveCopyAs target
MsgBox "Backup saved:" & vbLf & target, vbInformation
Exit Sub
Fail:
MsgBox "Backup failed: " & Err.Description, vbExclamation
End SubHow to use it
- Open the workbook and press
Alt+F11(Mac:Opt+F11orFn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it. - Choose Insert › Module. An empty code window opens on the right.
- Paste the whole code into that window. If the module already starts with
Option Explicit, do not paste that line twice. - Go back to Excel, press
Alt+F8(Mac:Opt+F8, or Tools › Macro › Macros), pickSaveBackupCopyand click Run. - Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.
To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.
How it works
SaveCopyAs writes a copy of the workbook, as it is in memory right now including unsaved changes, to a new file. Unlike SaveAs, it does not change which file you have open, so you keep working in the original. The copy has the same format as the original, which is why the extension is taken from the current name.
Dir(folder, vbDirectory) returns the folder name if the folder exists and an empty string if not; in that case MkDir creates it. InStrRev finds the last dot in the name, so a file called Plan v2.1.xlsx splits correctly into Plan v2.1 and .xlsx.
Format(Now, "yyyy-mm-dd_hh-nn-ss") builds the timestamp. Note nn for minutes, because mm means months in VBA format codes, and dashes instead of colons, which are not allowed in file names. The year-month-day order makes the backups sort by date in any file list. Date formats are explained in Working with dates and times in VBA.
Change it
To make a backup automatically every time you save, put this event in the ThisWorkbook module of the workbook (double-click ThisWorkbook in the Project pane) and keep SaveBackupCopy in a normal module of the same file:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
SaveBackupCopy
End SubBecause SaveCopyAs runs before the save, the backup holds the state just before saving; that is what you want in case the save goes wrong. Change ActiveWorkbook to ThisWorkbook in SaveBackupCopy when you use it this way. To save backups to another drive, set folder to a fixed path such as D:\Backups. To keep only the latest copy per day, use Format(Date, "yyyy-mm-dd"); later backups that day overwrite the earlier one. Events are covered in Events: macros that run by themselves.
What can go wrong
Dir and FileDateTime.For files stored in OneDrive or SharePoint, wb.Path may be a web address beginning with https://, and Dir and MkDir cannot work with that. Use a fixed local folder in that case. A copy saved next to the original does not protect you from a failed disk; keep important backups on a different drive or in cloud storage.
Related: VBA macro to save each sheet as a separate workbook in Excel, Files and folders: open, save, loop through files and write text files, Events: macros that run by themselves.