Create a Backup of a Workbook with Current Date

We all know that making backups of your work is important. Now you can have a macro do it for you. This simple macro can backup your workbook with today's date as part of the file name in the Same folder.

Macro Code (VBA Code)

Sub Backup()
'Step 1: Create a Backup of a Workbook with Current Date in the Same folder
     ThisWorkbook.SaveCopyAs _
     FileName:=ThisWorkbook.Path & "\" & _
     Format(Date, "mm-dd-yy") & " " & _
     ThisWorkbook.Name
End Sub

How This Macro Works

The trick to this macro is piecing together the new file name. The new file name has three pieces: the path, current date, and the original file name.

The path is captured by using the Path property of the ThisWorkbook object. Today's date is grabbed with the Date function.

You’ll notice that we are formatting the date (Format(Date, "mm-dd-yy")). This is because by default, the Date function returns mm/dd/yyyy. We use hyphens instead of forward slashes because the forward slashes would cause the file save to fail. (Windows does not allow forward slashes in filenames.)

The last piece of the new filename is the original filename. We use the Name property of the
ThisWorkbook object to capture that.

In the one and only step, the macro builds a new filename and uses the SaveCopyAs method to save the file in the Same folder.

How to Use This Macro

To use this macro, you can copy and paste it into a standard module:

  1. Activate the Visual Basic Editor by pressing ALT+F11.
  2. Right-click the project/workbook name in the Project window.
  3. Choose Insert -> Module.
    Insert Module
  4. Type or paste the code in the newly created module.

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>

4 comments
  1. AP
    Annie Paquette-Quintal

    Hi, would it be possible to add the time in the backup save?
    Thank you very much!

More comments