Private Sub personal_Backup()
' [URL]http://www.mrexcel.com/forum/excel-questions/864085-backing-up-personal-xlsb-using-visual-basic-applications.html#post4196704#2[/URL] & #14
Dim wbk As Workbook
Set wbk = Workbooks("Personal.xlsb")
Dim shtLog As Worksheet
Set shtLog = wbk.Worksheets("Log")
Dim tblLog As ListObject
Set tblLog = shtLog.ListObjects("tbl_Log")
Dim pvt As PivotTable
Set pvt = shtLog.PivotTables("pvt_Log")
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
Dim strBakPath As String
OptimizeVBA True
[B][COLOR=#800080]' ### If you aren't interested in the Logging function, delete code between two lines surrounded by ### (triple hashtags) -- the two subs that start with [U][I]tbl_[/I][/U] can also be deleted[/COLOR][/B]
' Builds an on going list of who saved file when.
' Code must go into the "ThisWorkbook" module of Personal.xlsb!
' All data is placed in column "A" adjust width and formats.
' [URL]http://www.mrexcel.com/forum/excel-questions/24014-visual-basic-applications-macro-tell-who-last-saved-file-when.html#9[/URL]
With shtLog
.Range("A1") = "Change Log: " & wbk.Name
.Range("A2") = "Created By"
.Range("B2") = "Created On"
.Range("A3") = Left$(wbk.BuiltinDocumentProperties("Author"), _
find_NthChar(" ", wbk.BuiltinDocumentProperties("Author"), 2) - 1)
.Range("B3") = Format(wbk.BuiltinDocumentProperties("Creation Date"), "mm/dd/yyyy")
.Range("C3") = Format(wbk.BuiltinDocumentProperties("Creation Date"), "hh:mm")
.Range("A5") = "Last Author"
.Range("B5") = "Last Saved"
' ~~ Log folder location of current file
.Hyperlinks.Add .Range("F1"), file_Path(wbk.FullName)
' ~~ Excel version: '=SUBSTITUTE( LEFT(CELL("filename",A1),FIND("]",CELL("filename",A1))-1),"[","") || _
[URL]https://exceljet.net/formula/get-workbook-name-and-path-without-sheet[/URL]
' ~~ Place Last Author and last time saved (date | time) into log table
tbl_addNewRow tblLog
tbl_addData lbt:=tblLog, _
rtsdata:=Left$(wbk.BuiltinDocumentProperties("Last Author"), _
find_NthChar(" ", wbk.BuiltinDocumentProperties("Last Author"), 2) - 1), _
col:=1
tbl_addData lbt:=tblLog, _
rtsdata:=Format(wbk.BuiltinDocumentProperties("Last Save Time"), "mm/dd/yyyy"), _
col:=2
tbl_addData lbt:=tblLog, _
rtsdata:=Format(wbk.BuiltinDocumentProperties("Last Save Time"), "hh:mm"), _
col:=3
' ~~ Remove duplicate log entries
.Range("tbl_Log[#All]").RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes ' [URL]https://msdn.microsoft.com/en-us/library/office/ff193823.aspx[/URL]
' ~~ Refresh pvt tracking personal updates
.PivotTables("pvt_Log").PivotCache.Refresh
End With
[COLOR=#800080]
[B]' ### End Logging function ###[/B][/COLOR]
If Application.StartupPath = [COLOR=#0000ff][I][B]Desktop path [where Personal.xlsb is located; see: [URL="http://wheatblog.com/2011/08/where-is-the-excel-personal-macro-workbook-located/"]Where is the Excel Personal Macro Workbook Located? – wheatblog[/URL]][/B][/I][/COLOR] Then
strBakPath = [COLOR=#0000ff][I][B]Desktop BACKUP path ' [update to your specific paths as Strings][/B][/I][/COLOR]
Else
strBakPath = Application.StartupPath & "\Archive" ' For laptop, other computers
If FSO.FolderExists(strBakPath) Then
GoTo SaveFile
Else
FSO.CreateFolder (strBakPath)
End If
End If
SaveFile:
With Workbooks("PERSONAL.xlsb")
.SaveCopyAs strBakPath & "PERSONAL.xlsb" & Format(Now, "_yyyymmdd[COLOR=#ff0000][B]_hhmm[/B][/COLOR].bak") [COLOR=#ff0000]<-- If you don't want to have multiple copies per day, eliminate the '_hhmm' section and you will only have one backup per day.[/COLOR]
.Save
End With
OptimizeVBA False
End Sub