Delete file in a folder with vba or override with new file

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim backupfolder As String


    backupfolder = "C:\BackupFile\"


    ThisWorkbook.SaveCopyAs Filename:=backupfolder & ThisWorkbook.Name
End Sub

Hi,
The above code is what I am using to create a backup of my data. I want it to override the file inside that backupfolder but it won't and hence throws an error. With three suggested reasons of which I fall into one; file already exists

So I am thinking of two options here:
1. Override it if possible with vba
2. Delete the previous file before creating new

Thanks in advance
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I tested your code and it does not show error. But I give you the options:

Option 1:Override it if possible with vba

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Application.DisplayAlerts = False
    Dim backupfolder As String
    backupfolder = "C:\BackupFile\"
    ThisWorkbook.SaveCopyAs Filename:=backupfolder & ThisWorkbook.Name
End Sub

Option 2: Delete the previous file before creating new

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Application.DisplayAlerts = False
    Dim backupfolder As String
    backupfolder = "C:\BackupFile\"
    If Dir(backupfolder & ThisWorkbook.Name) <> "" Then
        Kill (backupfolder & ThisWorkbook.Name)
    End If
    ThisWorkbook.SaveCopyAs Filename:=backupfolder & ThisWorkbook.Name
End Sub
 
Upvote 0
Great! Great!! Great!!!
Working now.

One last thing :

How do I avoid the code running when I open the file from the backup folder? That is when I have the backup opened, then don't create the backup since that will try to override that opened file. Which will result in errors.
 
Upvote 0
I've used this to avoid errors...
Code:
Public Function NoFileEr(Flpath As String)
'check if file exists. Copy to temp file from real file
Dim FS As Object, Temp3 As String
Set FS = CreateObject("Scripting.FileSystemObject")
Temp3 = Left(Flpath, Len(Flpath) - 4) & "T.xlsm"
FS.CopyFile Flpath, Temp3
Set FS = Nothing
End Function
Public Function BackToR(Retpath As String)
'if real source file open continue (leave source file open)
'if real transfer file open, close it and continue
'copy temp file back to real transfer file & kill temp file
Dim FS As Object, Temp2 As String
Temp2 = Left(Retpath, Len(Retpath) - 4) & "T.xlsm"
Set FS = CreateObject("Scripting.FileSystemObject")
On Error GoTo Errcode
FS.CopyFile Temp2, Retpath
Set FS = Nothing
Kill Temp2
Exit Function

Errcode:
On Error GoTo 0
FS.CopyFile Temp2, Retpath
Set FS = Nothing
Kill Temp2
End Function
Use something like...
Code:
NoFileEr ("C:\foldername\filename.xlsm")
BackToR ("C:\foldername\filename.xlsm")
HTH. Dave
 
Last edited:
Upvote 0
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim backupfolder As String
    backupfolder = "C:\BackupFile\"
    If backupfolder & ThisWorkbook.Name <> ThisWorkbook.FullName Then
        Application.DisplayAlerts = False
        ThisWorkbook.SaveCopyAs Filename:=backupfolder & ThisWorkbook.Name
        Application.DisplayAlerts = True
    End If
End Sub
 
Upvote 0
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim backupfolder As String
    backupfolder = "C:\BackupFile\"
    If backupfolder & ThisWorkbook.Name <> ThisWorkbook.FullName Then
        Application.DisplayAlerts = False
        ThisWorkbook.SaveCopyAs Filename:=backupfolder & ThisWorkbook.Name
        Application.DisplayAlerts = True
    End If
End Sub


Wow! !!!

Very cooler than I ever imagined.
Thanks
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

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