Backing up Personal.xlsb using VBA

Dr. Demento

Well-known Member
Joined
Nov 2, 2010
Messages
618
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I'm attempting to have Excel backup Personal.xlsb everytime I close Excel; I make so many changes each day (thanks to the hard work that y'all are doing, there's plenty to borrow from) and if I want to VPN into work, I can't get to my actual hard drive, whereas I have full access to the shared drive.

I've attempted to modify Jimmy Pena's work (shout out!), but I'm getting sporadic results; it worked once and now there are no errors to tell me what went wrong (even after commenting out the error suppression). After I got it to work once (not by closing Excel, but by running the macro (before making it _BeforeClose)), it wouldn't overwrite the existing file. After that, it stopped working altogether (although further futzing may have something to do with that :mad:).

I have it in the ThisWorkbook module. I'd like it to overwrite previous backup files without prompting (hence, DisplayAlerts), but I don't know if that's affecting it somehow.

Also, I tried including the filetype:=50 as suggest by RdB, but I have no idea how/where to insert that tidbit.

Any insights would be most appreciated.

And, as a random aside, is it normal when I save Personal that I have to save it twice? Personal.xlsb is hidden; if I save it once, the "cursor" disappears from the visible worksheet and doesn't re-appear until I save Personal a second time. Just curious.


Code:
' API to make a copy of a currently open file
Private Declare Function apiCopyFile Lib "kernel32" Alias "CopyFileA" _
(ByVal lpExistingFileName As String, _
ByVal lpNewFileName As String, _
ByVal bFailIfExists As Long) As Long

Private Sub Workbook_BeforeClose(Cancel As Boolean)
' [URL]http://www.jpsoftwaretech.com/updated-vba-backup-code/[/URL]

  On Error GoTo ErrorHandler
  
  Dim todaysDate As String

  Const fileName As String = "PERSONAL"
  Const fileType As String = ".xlsb"
  Const SourceFolder As String = "\\Shared Drive\AppData$\win7\User\AppData\Roaming\Microsoft\Excel\XLSTART\"
  Const DestinFolder As String = "\\Shared Drive\Documents\Computer Hacks\MS Office Hacks\Excel\Personal_xlsb\"
  Application.DisplayAlerts = False

    
  ' get current date and time
  todaysDate = Format(Now, "YYYYMMDD")
  ThisWorkbook.Save
 
  Call apiCopyFile(SourceFolder & fileName & fileType, DestinFolder & fileName & "_" & todaysDate & fileType, False)

  Application.DisplayAlerts = True

ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit

End Sub
 
I've left work, but I'm pretty sure this is it.

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

  Const sBak As String = "H:\something\something\Personal.xlsb"


  Application.DisplayAlerts = False
  With Workbooks("Personal.xlsb")
    .SaveCopyAs sBak & Format(Now(), "_yyyymmdd.bak")
    .Save
  End With
  Application.DisplayAlerts = True
End Sub

If I change it from a BeforeClose event to a regular macro, it works fine. Otherwise, the program closes but doesn't backup.
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
This is in the ThisWorkbook module for Personal, correct? Try this, and single-step when you hit the Stop statement:

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
  Const sBak        As String = "H:\something\something\Personal.xlsb"
  
  Stop

  If Not Me.Saved Then
    Application.DisplayAlerts = False
    Me.SaveCopyAs sBak & Format(Date, "_yyyymmdd.bak")
    Me.Save
    Application.DisplayAlerts = True
  End If
End Sub
 
Upvote 0
It appears that the BeforeClose event doesn't fire in Personal. Let me invite someone smarter than me into the thread to explain that, and suggest a workaround.

Also, this didn't work:

Code:
Option Explicit

Dim WithEvents appXl As Application

Private Sub Workbook_Open()
  Set appXl = Application
End Sub

Private Sub appXl_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
  Stop
  If Wb Is Me And Not Me.Saved Then BackupPersonal
End Sub

appXl doesn't persist past the Open event.
 
Last edited:
Upvote 0
Hello Shg
I tried your initial suggestion from a regular xlsm file and it worked fine.

When I opened Excel today, both the Personal.xlsb and the .bak file opened in the VBA list of open files.
I do not know why the Bak file shows.
Should I change something so the Bak is not active?

Thanks Dave
 
Upvote 0
Hi, Dave,

The backup needs to be someplace other than where Personal lives.
 
Upvote 0
I believe it's a bug though I can't recall an official KB article about it. I would suggest you change the process slightly and use the BeforeSave event instead. You'll only be prompted to save if there's actually something changed, and it should work to run the SaveCopyAs code too - just don't forget to disable events, then run the SaveCopyAs and then re-enable (just in case).
 
  • Like
Reactions: shg
Upvote 0
Rory, thank you.

What about appXl not persisting?
 
Upvote 0
What you might do is create a separate add-in that handles backing up the Personal macro workbook.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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