I am trying to create a macro that either works when closing or when saving to backup the file to another location.
Currently, the Macro I used is:
Private Sub Workbook_BeforeClose(Cancel As Boolean) 'Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) 'Saves the current file to a backup folder and the default folder 'Note that any backup is overwritten Application.DisplayAlerts = False ActiveWorkbook.SaveCopyAs Filename:="T:\TEC_SERV\Backup file folder - DO NOT DELETE\" & _ ActiveWorkbook.Name ActiveWorkbook.Save Application.DisplayAlerts = True End Sub
This backs up the file for the first time, however, if it is repeated, I get:
Runtime Error '1004'; Microsoft Office Excel cannot access the file "T: \ TEC_SERV \ Backup file folder - DO NOT DELETE \ Test Macro Sheet.xlsm. There are several possible reasons:
File name or path does not exist
The file is being used by another program.
The book you are trying to save has the same name as ...
I know that the path is correct, I also know that the file is not open anywhere. The workbook has the same name as me, which I am trying to save, but should just overwrite.
Any help would be greatly appreciated.
source share