Backups

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a spreadsheet that is going to record data that I don't want to lose. Therefore, I want to insert a feature that runs a daily backup and saves a copy of the entire workbook as a separate file.
  • I need the file saved with the filename "Allocation sheets - dd.mm.yyyy".
    • The dd.mm.yyyy needs to be the date the backup is run.
  • I want it run when the spreadsheet is first opened for a day and if it isn't opened, I don't want a backup for that day.
  • It needs to be stored in a folder called Backup that is in the same directory as the original file.
Can someone please help me with the vba to add to the spreadsheet to achieve this?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Put this code in the ThisWorkBook (Code).

The documentation is included in the code.

You can download this file at ComputerHotShot.com .



VBA Code:
Dim BackupFileName, CurrentWorkbook, CreateABackup, OnlyOneWorkBook, WorkBookIsABackup

Private Sub Workbook_Open()
     
        'Assumptions:
        '1. The Folder 'BACKUP' exists. This code does NOT create the folder if it doesn't exist.
        '2. The Master Workbook does NOT have a '-' in the name.
        '    You can change the BACKUP Workbook delimiter here.
    BackUpFileDelimiter = "-"
        '
        'Possibilities:
        '1. If this is the first time TODAY the Master Workbook has been opened,
        '         then create a BACKUP Workbook and notify user.
        '2. If this is NOT the first time TODAY the Master Workbook has been opened,
        '         then do NOT create a BACKUP Workbook and notify user.
        '    This could result in LOST DATA if the Master Workbook is changed
        '            as the BACKUP was created the FIRST time Master Workbook was opened.
        '3. The user opens the BACKUP Workbook.  As the BACKUP Workbook has this code,
        '    the code needs to allow the BACKUP Workbook to be opened without incident.
        '    If the Master Workbook is open when the BACKUP Workbook is opened the
        '       BACKUP Workbook is closed to be certain the user doesn't make changes to
        '       the BACKUP Workbook thinking they are making changes to the Master Workbook.
        '    If you want to open the BACKUP Workbook, close all other Workbooks first.
        
        'Backup file format "BackUpFileDelimiter dd.mm.yyyy.xlms" or most likely " - dd.mm.yyyy.xlms"
        FormatSaveDate = " " & BackUpFileDelimiter & " " & Right("0" & Day(Now()), 2) & "." & Right("0" & Month(Now()), 2) & "." & Year(Now()) & ".xlsm"
             
        BackupFileName = Application.ActiveWorkbook.Path & "\Backup\" & Replace(Application.ActiveWorkbook.Name, ".xlsm", "") & FormatSaveDate
        
        'Is there a Backup Workbook already open? If so, close it and continue.
        OnlyOneWorkBook = "NO"
        Call CloseOpenBackupWorkbook(BackUpFileDelimiter)
        
        If OnlyOneWorkBook = "YES" And WorkBookIsABackup = "YES" Then 'Only one workbook and it is a backup
            MsgBox "This is a BACKUP Workbook so NO NEED to created a BACKUP." & _
            vbCrLf & vbCrLf & "Be CAREFUL as changes to this BACKUP Workbook will NOT be included in the Master Workbook."
            Exit Sub
        End If
        
        'Is there already a Backup Workbook? If so, skip saving a new one.
        Call DoesWorkbookExist
        
        If CreateABackup = "NO" Then
            MsgBox "This is already BACKUP Workbook so NO Backup has been created." & _
            vbCrLf & vbCrLf & "Be CAREFUL as changes to this Workbook are not included in the BACKUP Workbook created earlier today."
            Exit Sub                    'Backup Workbook exists, we are done here
        End If
        
        CurrentWorkbook = Application.ActiveWorkbook.Name
        On Error Resume Next
            ActiveWorkbook.SaveAs Filename:=BackupFileName
        If Err.Number <> 0 Then
            MsgBox "Error: " & Err.Description & vbCrLf & vbCrLf & "Most likely the BackUp Folder does NOT Exist." & _
            vbCrLf & vbCrLf & "The program will stop now."
            Exit Sub
        End If
        
        
        MsgBox "A BACKUP Workbook has been created." & vbCrLf & vbCrLf & BackupFileName
        
        'Non backup path
        RemoveBackupFolder = Replace(Application.ActiveWorkbook.Path, "\Backup", "")
        
        SaveOriginalFule = RemoveBackupFolder & "\" & CurrentWorkbook
        
        Workbooks.Open Filename:=SaveOriginalFule

End Sub

Sub CloseOpenBackupWorkbook(BackupDelimiterCharacter)

    Dim wb As Workbook
    
    WorkbookCounter = 0
    WorkbooktoClose = ""
    WorkBookIsABackup = "NO"
    
    For Each wb In Application.Workbooks
      If InStr(wb.Name, BackupDelimiterCharacter) > 0 Then
          WorkbooktoClose = wb.Name
          WorkBookIsABackup = "YES"
      End If
      WorkbookCounter = WorkbookCounter + 1
    Next wb
    
    If WorkbookCounter = 1 Then 'only ONE Workbook open don't close it
        OnlyOneWorkBook = "YES"
    Else
        OnlyOneWorkBook = "NO"  'MORE than ONE Workbook open
        If WorkBookIsABackup = "YES" Then
            Workbooks(WorkbooktoClose).Close SaveChanges:=False  'close the BACKUP Workbook
        End If
    End If
    
End Sub

Sub DoesWorkbookExist()
  
    If Dir(BackupFileName) = "" Then 'Check Workbook exists or not
        CreateABackup = "YES" 'Workbook is not available
    Else
        CreateABackup = "NO"    'Workbook is available
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,582
Members
449,089
Latest member
Motoracer88

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