VBA Backup deletes original

nparsons75

Well-known Member
Joined
Sep 23, 2013
Messages
1,254
Office Version
  1. 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.

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
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
If I remove that will it effect it in any other way? It looks like it deletes the original and then is supposed to save a new one, it looks like it kills the old one to avoid the message... A file named.....already exists, do you want to replace it!!
 
Last edited:
Upvote 0
No it shouldn't affect it in anyway.... try commenting out the 2 lines:

Code:
Kill savedName
ThisWorkbook.SaveAs savedName

there is no point in saving the file again as you haven't made any changes since running the macro
 
Upvote 0
That stops the message and saves a backup, however it does not actually save the file now. How odd....
 
Upvote 0
Your code is running on the AfterSave event - if it doesn't save then the code doesn't run.
 
Upvote 0
Ah, I see, so what would prevent it saving? most of the time it does, sometimes it must fail as the file disappears?
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,921
Members
449,094
Latest member
teemeren

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