To save an Excel file with the current date and time in its name using VBA, use the Format function to build a timestamp string and pass it to ThisWorkbook.SaveAs. For example: ThisWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\Report_" & Format(Now, "yyyy-mm-dd_hh-mm-ss") & ".xlsx". This creates a new file each time the macro runs, so you keep a version history without overwriting the previous save. You will need a macro-enabled workbook (.xlsm) and basic familiarity with the VBA editor.

What You'll Need

  • Excel with macros enabled (Windows or Mac). The steps below use the standard VBA editor; labels may differ slightly by version or region.
  • Your workbook saved as .xlsm (macro-enabled) so the code persists.
  • A folder path where you have write permission. Using ThisWorkbook.Path keeps the new file beside the original.
  • Optional: a button or keyboard shortcut to run the macro easily.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (Windows) or Option+F11 (Mac). In the Project Explorer, right-click your workbook and choose Insert > Module. A new module appears.
  2. Paste the core macro. In the module, enter:
    Sub SaveWithDateTime()
        Dim ts As String
        ts = Format(Now, "yyyy-mm-dd_hh-mm-ss")
        ThisWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\Report_" & ts & ".xlsx", FileFormat:=xlOpenXMLWorkbook
    End Sub
    This saves a copy named like Report_2025-03-21_14-30-05.xlsx in the same folder as the original.
  3. Adjust the name and format. Change "Report_" to your preferred prefix. The format string controls the timestamp: yyyy year, mm month, dd day, hh hour (24-hour), mm minutes, ss seconds. Use hyphens or underscores instead of colons and slashes because Windows filenames cannot contain : / \ * ? " < > |.
  4. Handle the file format correctly. If your workbook contains macros and you want to keep them, save as .xlsm with FileFormat:=xlOpenXMLWorkbookMacroEnabled. Saving as .xlsx strips macros from the new file. If you only need values, .xlsx is fine.
  5. Test the macro. Press F5 while the cursor is inside the subroutine. Check the folder for the new file. If Excel prompts about overwriting, your timestamp may be too coarse (for example, only date). Include seconds or use Now with a full timestamp.
  6. Run it from a button (optional). On the sheet, go to Developer > Insert > Button (Form Control), draw the button, and assign the macro. On Mac, use Developer > Insert > Button. This makes saving a one-click action.
  7. Save the workbook itself. The macro saves a copy; your original remains open. To also save changes to the original, add ThisWorkbook.Save after the SaveAs line, but note that SaveAs changes the active file to the new copy. If you want to keep working in the original, use ThisWorkbook.SaveCopyAs instead, which writes a copy without switching the active workbook.

Common Mistakes

  • Using illegal filename characters. Colons and slashes from time formats like hh:mm:ss cause runtime error 1004. Replace them with hyphens or underscores.
  • Forgetting the file extension. Omitting .xlsx or .xlsm can produce a file without a proper extension, or Excel may append one automatically. Always include it in the string.
  • Saving as .xlsx when macros are needed. The new file will not contain the VBA code. Use .xlsm and the matching FileFormat constant.
  • Assuming ThisWorkbook.Path is valid. If the workbook has never been saved, Path is empty and the save fails. Save the workbook first or hard-code a folder path.
  • Overwriting because the timestamp is too coarse. Using only the date means running twice in one day overwrites the first file. Include hours, minutes, and seconds, or add a counter.
  • Confusing SaveAs and SaveCopyAs. SaveAs switches the active workbook to the new file; SaveCopyAs leaves the original active. Choose based on whether you want to continue in the original.

Tips

  • Use Format(Now, "yyyy-mm-dd_hh-mm-ss") for a sortable timestamp. Year first ensures files sort chronologically in File Explorer.
  • If you need a folder that does not exist, create it first with MkDir; otherwise the save fails.
  • For a user-friendly prompt, use Application.GetSaveAsFilename to let the user pick a location, then append the timestamp.
  • Store the folder path in a cell or a constant at the top of the module so you can change it without editing code.
  • On Mac, the VBA editor shortcuts differ, but the SaveAs method and Format function work the same.

Troubleshooting

Runtime error 1004: “Method 'SaveAs' of object '_Workbook' failed”. Check the path exists, you have write permission, and the filename contains no illegal characters. Also ensure the workbook is not open in another instance of Excel.

The file saves but has no extension or the wrong one. Verify the extension is inside the quoted string and matches the FileFormat constant. For .xlsm, use xlOpenXMLWorkbookMacroEnabled.

Macros are missing from the saved copy. You saved as .xlsx. Change the extension to .xlsm and the format constant accordingly.

The macro does nothing. Ensure macros are enabled (File > Options > Trust Center > Trust Center Settings > Macro Settings). Also confirm the code is in a standard module, not a sheet module, and that you pressed F5 with the cursor inside the subroutine.

It overwrites the previous file. Your timestamp lacks seconds or you are using a static name. Include ss in the format string or add a unique counter.

Path is empty. Save the workbook once before running the macro, or replace ThisWorkbook.Path with an explicit folder path like "C:\Reports".

Frequently Asked Questions

Can I include the time with seconds in the filename?

Yes. Use Format(Now, "yyyy-mm-dd_hh-mm-ss"). Avoid colons because Windows does not allow them in filenames.

How do I save as .xlsm instead of .xlsx?

Set the extension to .xlsm and use FileFormat:=xlOpenXMLWorkbookMacroEnabled in the SaveAs call.

What is the difference between SaveAs and SaveCopyAs?

SaveAs changes the active workbook to the new file. SaveCopyAs writes a copy but keeps the original active, which is useful when you want to continue working in the original.

Why do I get error 1004 when saving?

Common causes are illegal characters in the filename, a non-existent folder, lack of write permission, or an unsaved workbook with an empty Path.

Can I use a custom date format like “dd-mm-yyyy”?

Yes. Any valid VBA format string works, but stick to characters allowed in filenames: letters, digits, hyphens, underscores, and spaces.

Does this work on Excel for Mac?

Yes. The VBA SaveAs method and Format function work on Mac. The VBA editor shortcut is Option+F11 instead of Alt+F11.