VBA to create backup file when closing excel

sparky_13

New Member
Joined
Nov 21, 2016
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi,
I'm am extremely green using VBA. I was able to find info on creating a macro in VBA that would save a copy of my workbook to another location, as a backup; and I was able to modify where I wanted to save it. I linked this macro to a Button in my work book.

What I am looking for is, instead of clicking the button, can I add some code so that the macro runs when I hit the 'X' button to close excel?

VBA Code:
Sub Backup_Files()
ActiveWorkbook.SaveCopyAs "D:\Time Sheets\BackUp_" + ActiveWorkbook.Name
End Sub
 

Attachments

  • backup_vba.JPG
    backup_vba.JPG
    50.5 KB · Views: 3

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
add below to ThisWorbook module
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean) 'Fire when user closes Workbook
ActiveWorkbook.SaveCopyAs "D:\Time Sheets\BackUp_" + ActiveWorkbook.Name
End Sub
 
Upvote 0
Solution
add below to ThisWorbook module
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean) 'Fire when user closes Workbook
ActiveWorkbook.SaveCopyAs "D:\Time Sheets\BackUp_" + ActiveWorkbook.Name
End Sub
I've replaced my code with yours. When I close my test file, no "backup" file is made.

Like I said, I'm VERY new to VBA
 
Upvote 0
Ahhh, so not in a module. That seems to work with a test file.

I will test in my main file as well.
 
Upvote 0
you are welcome but just so you know it will only save that file once.
there is nothing in the code that will overwrite already existing back up.
 
Upvote 0
you are welcome but just so you know it will only save that file once.
there is nothing in the code that will overwrite already existing back up.
I added some text in a cell in my original file and saved.
closed the original file.
opened the "backup" file that the VBA saved as a copy, and confirmed the text was there.
closed the "backup" file.
opened original file, added more text in another cell and saved.
closed the original file.
opened the "backup" file that the VBA saved, 2nd cell text was there.

Still only have (1) "backup" file in the 'D:\Time Sheets' location.

So for me it seems to be working they way I was looking for.
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,138
Members
449,098
Latest member
Doanvanhieu

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