Same File Name, New Location

Muleskin57

New Member
Joined
Dec 22, 2016
Messages
23
I've created a tool to manage resources for disabled persons in a group home setting. It will be used by non-excel users mostly. I'm trying to create a macro that saves the existing file to a backup directory before exiting. Anyone have experience with this? I'm hitting my frustration point.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
You can use the Workbook_BeforeClose event procedure, which is VBA code that automatically run when they try to close the workbook.
We can capture the file name, and have it automatically save to some pre-determined backup location (hard-coded in the VBA code).

Paste the VBA code below in the "ThisWorkbook" module in VBA (it HAS to be in that module to work automatically).
Change the file path to wherever you want it saved.
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    Dim buPath As String
    Dim wbName As String

'   Enter backup file path (ending in a "\")
    buPath = "C:\Temp\"

'   Capture just the name of the current file
    wbName = ActiveWorkbook.Name
    
'   Save file to backup directory
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs buPath & wbName
    Application.DisplayAlerts = True
    
End Sub
Also note that they must have Macros/VBA enabled for this code (or any automated VBA code) to work.
 
Upvote 0
That's great! If the file is to be saved on a subfolder of it's current location(subfolder named "backup"), how would I state that? Multiple locations would be using this form on a network location specific to each home. We have write only sub folders (backup) that we want to dump the file into after the month end macro runs.
 
Upvote 0
If the file is to be saved on a subfolder of it's current location(subfolder named "backup"), how would I state that?
Just change this line:
Code:
buPath = "C:\Temp\"
to
Code:
buPath = ActiveWorkbook.Path & "\backup\"
 
Upvote 0
You are welcome!
Glad I was able to help.
:)
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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