Backup creation upon open

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,392
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I want to create a backup of a file once I open it.

  • The backup needs to be done the first time the file is opened each week, but only after a Wednesday, so Wednesday, Thursday or Friday.
  • I only need it once per week and for the backup, just make a copy of the file.
  • The location of the backup files need to be stored in a backup folder that is located in the current directory of the spreadsheet. It then needs to be separated by folders of year and month.
  • The file name for the backup file needs to be "the name of the current file - the date the backup is run", for instance, "Sample_file - dd.mm.yy"
Thanks
 
VBA Code:
CurrentDate = Format(Now, "yyyy-mm-dd hhmm")

or

VBA Code:
CurrentDate = Format(Now, "yyyy-mm-dd hh.mm")
 
Last edited:
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Thank you Mark, that is perfect!! :biggrin:

For anyone who is reading this thread looking for an answer, here is my final, working code:

VBA Code:
Sub Save_Spreadsheet()
    Dim CurrentDate As String
    Dim FilePath As String
    Dim FileName As String
    
    
    CurrentDate = Format(Now, "dd.mm.yy hh.mm AM/PM")
    

    FilePath = "C:\Users\....\OneDrive - .....\Reception Folder\Qualifications\Backups\"
    FileName = "Quals Expired and soon to expire backup - "
    
    ActiveWorkbook.SaveAs FilePath & FileName & CurrentDate & ".xlsm"

End Sub

The .... in the code are just blanking out the organisation name for privacy reasons.


The above code is stored in a module and ran when the workbook opens.
 
Upvote 0
I just thought of something it needs to do Mark. I need it to check if the backup exists before copying and only copy if the backup doesn't exist.
 
Upvote 0
Try the syntax below
VBA Code:
    Dim FilePath As String, FileExists As String
    FilePath = "yourfull_path_and_name_inc_extension"

    FileExists = Dir(FilePath)

    If FileExists = "" Then
        MsgBox "File doesnt exist"
    Else
        MsgBox "File exists"
    End If
 
Upvote 0
Thanks Mark, how would I update my code to add what you suggest? There is also another question below the code.

VBA Code:
Sub Save_Spreadsheet()
    Dim CurrentDate As String
    Dim FilePath As String
    Dim FileName As String
    
    
    CurrentDate = Format(Now, "dd.mm.yy hh.mm AM/PM")
    

    FilePath = "C:\Users\...\OneDrive - ...\Reception Folder\Qualifications\Backups\"
    FileName = "Quals Expired and soon to expire - Backup - "
    
    ActiveWorkbook.SaveAs FilePath & FileName & CurrentDate & ".xlsm"

End Sub


In addition to this issue, the code saves the backup file with the backup name but then you have the backup file open and any changes are made to the backup file instead of the working file. I need to save a copy of it in the background but then to keep working on the original file.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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