LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 137
Default How to create backups of your workbooks as you open and as you close

If you're working on an important workbook and wish to keep a running list with separate incremental backups, you can copy this code to your 'ThisWorkbook' section in the VBE

Note: This will not be executed if the file is opened as READONLY (what's the point, right?)
Note: A backup is automatically made upon opening the workbook (not if the file was opened READONLY)
Note: This creates a full backup of the workbook including all macros
Note: Backups are identified with a Date and Time suffix helping to identify them
Note: Upon Saving or Closing you are prompted for an optional comment which might help to identify the changes you made during your edit.


Setup: Change the BackupDir const to your target folder (which must exist)


'**** Place in ThisWorkbook
'**** Copy from here down

Option Explicit

'Author: Greg Glynn

Const BackupDir = "P:\Backups\" '*** Change this to your backups folder which can be different from your workbook folder

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim BackupFileName As String, BackupDateTime As String, BackupUser As String, BackupFileType As String
Dim sh As Worksheet, wkb As Workbook
Dim Comment As String

Comment = ""

Application.Caption = "*** Auto Backup ***"
DoEvents
Application.EnableEvents = False
Application.ScreenUpdating = False

'Backup the data if the sheet was not opened in ReadOnly mode
If ThisWorkbook.ReadOnly = False Then

Comment = InputBox("Add a comment?")
If Comment < "" Then
Comment = " - " & Replace(Comment, "/", "-")
End If

'Create a backup
BackupFileName = Left(ActiveWorkbook.Name, InStr(ActiveWorkbook.Name, ".xls") - 1)
BackupDateTime = " " & Format(Now(), "YYYY-MM-DD hh-mm-ss")
BackupUser = " " & Environ$("Username")
BackupFileType = "." & Mid(ActiveWorkbook.Name, InStr(ActiveWorkbook.Name, ".xls") + 1, 999)
On Error Resume Next
ActiveWorkbook.SaveCopyAs BackupDir & BackupFileName & BackupDateTime & BackupUser & Comment & BackupFileType

Application.StatusBar = "Saved " & BackupFileName & " " & Format(FileLen(BackupDir & BackupFileName & BackupDateTime & BackupUser & Comment & BackupFileType), "#,#") & " bytes."
On Error Resume Next
'Will be saved anyway by virtue of the "Workbook_BeforeSave" function
'ActiveWorkbook.Save

End If

Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Caption = ""
End Sub

Private Sub Workbook_Open()

Dim BackupFileName As String, BackupDateTime As String, BackupUser As String, BackupFileType As String
Dim sh As Worksheet, wkb As Workbook

Application.Caption = "*** Auto Backup ***"
DoEvents
Application.EnableEvents = False
Application.ScreenUpdating = False

'Backup the data if the sheet was not opened in ReadOnly mode
If ThisWorkbook.ReadOnly = False Then

'Create a backup
BackupFileName = Left(ActiveWorkbook.Name, InStr(ActiveWorkbook.Name, ".xls") - 1)
BackupDateTime = " " & Format(Now(), "YYYY-MM-DD hh-mm-ss")
BackupUser = " " & Environ$("Username")
BackupFileType = "." & Mid(ActiveWorkbook.Name, InStr(ActiveWorkbook.Name, ".xls") + 1, 999)
On Error Resume Next
ActiveWorkbook.SaveCopyAs BackupDir & BackupFileName & BackupDateTime & BackupUser & BackupFileType
On Error Resume Next
ActiveWorkbook.Save

End If

Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Caption = ""

End Sub
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Open and Close Workbooks Tontonan Excel Programming 2 February 18th 09 09:41 PM
Close all other open Workbooks CLR Excel Programming 4 December 5th 08 04:57 PM
Code to close many open workbooks MurrayB Excel Programming 13 September 18th 08 01:32 PM
close all open workbooks except the active one Dav[_4_] Excel Programming 3 April 6th 06 01:26 AM
Open Close workbooks bbc1 Excel Discussion (Misc queries) 2 August 28th 05 11:24 AM


All times are GMT +1. The time now is 07:19 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright 2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"