Hi to you all
My first post so be gentle with me....
I'm having a problem that I can't seem to figure out. I'm new to the VBA programming side of excel and have ended up with a nice little spreadsheet to learn with.
On closing my workbook I wish to create a backup of the speadsheet with it's name and the date appended to it in a backup folder. Then save it again using the original name (overwrite) in the original folder. I'd like to control the save myself and not have the prompt for save appear.
I've managed to get the desired results (although I can not stop the save prompt from apprearing) but, on occasion I get an error when saving it..
The thing I can't seem to get is that some times it does seem to work...
I hope I've managed to explain it clearly and hope that I'm not being dense....
Thanks in advance for your help and suggestions.
Regads to you all.
My first post so be gentle with me....
I'm having a problem that I can't seem to figure out. I'm new to the VBA programming side of excel and have ended up with a nice little spreadsheet to learn with.
On closing my workbook I wish to create a backup of the speadsheet with it's name and the date appended to it in a backup folder. Then save it again using the original name (overwrite) in the original folder. I'd like to control the save myself and not have the prompt for save appear.
I've managed to get the desired results (although I can not stop the save prompt from apprearing) but, on occasion I get an error when saving it..
Run-Time Error 1004 : Method 'SaveAs' of object '_Workbook' failed
with the offending line highlighted in debug
Code:
Sub Save_Close()
Application.DisplayAlerts = False
Dim SaveDate As String
SaveDate = Format(Date, "DD-MM-YYYY")
[COLOR=#008000]'Create the present save location[/COLOR]
SaveLocation = ThisWorkbook.Path & "\" & ActiveWorkbook.Name
[COLOR=#008000]'Check to see if a backup folder exists and create it if not[/COLOR]
CheckBackupFolder = ThisWorkbook.Path
If Dir(CheckBackupFolder & "\Backup\") = "" Then
MkDir CheckBackupFolder & "\Backup\"
End If
[COLOR=#008000]'Create the backup path[/COLOR]
BackupLocation = ThisWorkbook.Path & "\Backup\" & Mid(ActiveWorkbook.Name, 1, InStr(1, ActiveWorkbook.Name, "_") - 1) & "_" & SaveDate & ".xlsm"
[COLOR=#008000]'Check to see if it's backup day and save in backup folder if true[/COLOR]
If Weekday(Date) = ActiveWorkbook.Sheets("Input").Range("X66") Then
ActiveWorkbook.SaveAs Filename:=BackupLocation, FileFormat:=52, CreateBackup:=False
End If
[COLOR=#008000]'Save Spreadsheet in it's default location[/COLOR]
[COLOR=#b22222] ActiveWorkbook.SaveAs Filename:=SaveLocation, FileFormat:=52, CreateBackup:=False[/COLOR][COLOR=#0000ff]
[/COLOR]
Application.DisplayAlerts = True
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
End Sub
The thing I can't seem to get is that some times it does seem to work...
I hope I've managed to explain it clearly and hope that I'm not being dense....
Thanks in advance for your help and suggestions.
Regads to you all.