excelstarter1
Board Regular
- Joined
- Jul 20, 2017
- Messages
- 81
Hey guys,
I coded the following backup routine in VBA last month and just when I needed one of those (presumably working) backup files I noticed the error I made... Stupid rookie mistake I guess.
Basically the code was intended to save a copy of the active workbook with a time/date stamp in a folder called 'Backup'. However, up until now, I did not know, that I cannot force the fileformat when using the command ActiveWorkbook.SaveCopyAs.
I am running Excel 2010, maybe the issue is solved in newer versions of Excel. Anyways, it would be great if a more experienced board user could help me out with this and revise the code. Thinking about it I dont want to force the file format 'xlsx' but instead I just want to keep the original file format (xls, xlsm, csv, ...) and add the time stamp to the original workbook when saving.
Thank you very much in advance!!
Regards
I coded the following backup routine in VBA last month and just when I needed one of those (presumably working) backup files I noticed the error I made... Stupid rookie mistake I guess.
Basically the code was intended to save a copy of the active workbook with a time/date stamp in a folder called 'Backup'. However, up until now, I did not know, that I cannot force the fileformat when using the command ActiveWorkbook.SaveCopyAs.
I am running Excel 2010, maybe the issue is solved in newer versions of Excel. Anyways, it would be great if a more experienced board user could help me out with this and revise the code. Thinking about it I dont want to force the file format 'xlsx' but instead I just want to keep the original file format (xls, xlsm, csv, ...) and add the time stamp to the original workbook when saving.
Thank you very much in advance!!
Regards
Code:
Option Explicit
Sub Create_Backup()
Dim wkbname As String, wkbpath As String, filenm As String
Application.DisplayAlerts = True
wkbname = ActiveWorkbook.Name
wkbpath = ActiveWorkbook.Path
filenm = CreateObject("Scripting.FileSystemObject").GetBaseName(wkbname)
If Dir(wkbpath & "\" & "Backup", vbDirectory) = "" Then 'check if Backup folder already exists, if not > create folder
If MsgBox("The folder or path " & vbNewLine & vbNewLine & wkbpath & "\" & "Backup" & vbNewLine & vbNewLine & "does not exist." & vbNewLine & vbNewLine & _
"Want to create the backup folder?", (vbYesNo)) = vbNo Then Exit Sub
MkDir wkbpath & "\" & "Backup"
ActiveWorkbook.SaveCopyAs Filename:=wkbpath & "\" & "Backup" & "\" & filenm & "_" & Format(Now, "yyyy-mm-dd_hh-mm") & ".xlsx"
Else
ActiveWorkbook.SaveCopyAs Filename:=wkbpath & "\" & "Backup" & "\" & filenm & "_" & Format(Now, "yyyy-mm-dd_hh-mm") & ".xlsx"
End If
CreateObject("WScript.Shell").Popup "Auto backup created", 1, "Backup"
End Sub
Last edited: