nparsons75
Well-known Member
- Joined
- Sep 23, 2013
- Messages
- 1,254
- Office Version
- 2016
Hi all,
I am using some code which I was given to create a backup of a file I use regularly. Intermittently when the backup takes place, the file is backed up but the original file disappears. Its very odd.
The code is as follows, hopefully someone can see why it happens. Thanks in advance.
I am using some code which I was given to create a backup of a file I use regularly. Intermittently when the backup takes place, the file is backed up but the original file disappears. Its very odd.
The code is as follows, hopefully someone can see why it happens. Thanks in advance.
Code:
Private Sub Workbook_AfterSave(ByVal Success As Boolean)If Not Success Then Exit Sub
Const backupFolder = "C:\MAIN FILE\BACKUPS"
Dim savedName As String
Dim backupName As String
Dim dotFinder As Long
Application.EnableEvents = False
savedName = ThisWorkbook.FullName
backupName = backupFolder & Mid$(savedName, InStrRev(savedName, "\"))
dotFinder = InStrRev(backupName, ".")
backupName = Left$(backupName, dotFinder) & Format$(Now(), "yyyymmddhhnnss.") & Mid$(backupName, dotFinder + 1)
If backupName = savedName Then Exit Sub
If Dir$(backupName) <> "" Then Kill backupName
ThisWorkbook.SaveAs backupName
Kill savedName
ThisWorkbook.SaveAs savedName
Application.EnableEvents = True
End Sub