Macro to auto create backup workbook to another folder upon saving the original

Raceman

Board Regular
Joined
Mar 11, 2010
Messages
64
Hi, I haven't been able to figure out a macro that , upon saving my file, will automatically create a backup file (my original file name plus "backup" added to the end), and save it to my desktop. Outside of this macro, my assumption is that each time the backup is saved there will be a message that says something like: "do want to replace the existing backup file on my desktop", that works ok for me. It's ok if it doesn't ask the question too.

Of course I should see no difference in the way my original file is saved. I will click the save button, my original file will save in the usual place and the backup will replace the previous backup file on my desktop.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
.
.

Place this event-handler procedure in the code module ThisWorkbook within your workbook:

Code:
Private Sub Workbook_AfterSave(ByVal Success As Boolean)

    Dim sPath As String
    Dim vName As Variant
    
    sPath = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    vName = Split(Me.Name, ".")
    
    With Application
        .DisplayAlerts = False
        .EnableEvents = False
    End With
    
    Me.SaveCopyAs _
        Filename:= _
            sPath & Application.PathSeparator & _
            vName(0) & "_backup." & vName(1)
    
    With Application
        .DisplayAlerts = True
        .EnableEvents = True
    End With

End Sub
 
Last edited:
Upvote 0
I am probably doing something wrong, but this didn't work. Am I suppose to substitute my name into the code somewhere? What is "wScript.Shell"?
 
Upvote 0
I am probably doing something wrong, but this didn't work. Am I suppose to substitute my name into the code somewhere? What is "wScript.Shell"?


You must place this procedure in the code module ThisWorkbook within your workbook.

Then, whenever you save your workbook, it will create the backup you wanted.
 
Upvote 0
I have placed it in This Workbook section on a test file and when I save my file nothing saves to my desktop as a Backup file. I did the test when I first received your post and again today and nothing happens (that I am aware of anyway)
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,102
Members
452,379
Latest member
IainTru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top