find, delete, and save a new file

sparky_13

New Member
Joined
Nov 21, 2016
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hello,

I previously, with help, created a macro to back up my timesheet:
VBA Code:
Sub Backup_Files()
ActiveWorkbook.SaveCopyAs "D:\Time Sheets\BackUp_" + ActiveWorkbook.Name
End Sub

This code worked for a while, but started to get buggy the farther into the year I get (timesheet is for the whole year). My research has got me to be able to write a macro that will search for my back up file, if it doesn't exist it will save it, and if it does exist will delete it.
VBA Code:
Sub Existing_Backup_File()
    Dim FileName As String
    FileName = VBA.FileSystem.Dir("D:\Time Sheets\BackUp_" + ActiveWorkbook.Name)
    If FileName = VBA.Constants.vbNullString Then
        ActiveWorkbook.SaveCopyAs "D:\Time Sheets\BackUp_" + ActiveWorkbook.Name
    Else
        Kill "D:\Time Sheets\BackUp_" + ActiveWorkbook.Name
    End If
End Sub

I am looking to save a new backup copy after deleting it. I thought I was on the right track with this code:
VBA Code:
Sub Existing_Backup_File2()
    Dim FileName As String
    FileName = VBA.FileSystem.Dir("D:\Time Sheets\BackUp_" + ActiveWorkbook.Name)
    If FileName = VBA.Constants.vbNullString Then
        ActiveWorkbook.SaveCopyAs "D:\Time Sheets\BackUp_" + ActiveWorkbook.Name
    Else
        If FileExist("D:\Time Sheets\BackUp_" + ActiveWorkbook.Name) = True Then
        Kill "D:\Time Sheets\BackUp_" + ActiveWorkbook.Name
        Else: ActiveWorkbook.SaveCopyAs "D:\Time Sheets\BackUp_" + ActiveWorkbook.Name
        End If
    End If
End Sub

But it seems that the "If FileExist" is not defined. I'm not sure where to go from here. Any help is greatly appreciated.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
My thought process was kinda correct, but I found much easier code:
VBA Code:
Sub Backup_File4()
Dim FilePath As String
Dim FileName As String
Dim FullPath As String

FilePath = "D:\Time Sheets\"
FileName = "BackUp_" + ActiveWorkbook.Name
FullPath = "D:\Time Sheets\BackUp_" + ActiveWorkbook.Name

If Dir(FullPath) <> "" Then
Kill FullPath
End If

'Save a new file
ActiveWorkbook.SaveCopyAs "D:\Time Sheets\BackUp_" + ActiveWorkbook.Name
End Sub

So far this seams to work like a champ!
 
Upvote 0

Forum statistics

Threads
1,226,112
Messages
6,189,040
Members
453,521
Latest member
Chris_Hed

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