Knockoutpie
Board Regular
- Joined
- Sep 10, 2018
- Messages
- 116
- Office Version
- 365
- Platform
- Windows
Hi all, ran across some code a while back from reddit on how to backup the PERSONAL.XLSB file with every save, it's pretty nifty!
I'm attempting to modify the code to where if the first file path is unavailable, then it will save the backup to the second file path.
I think i'm close, but when I disconnect from wifi to detach from the network drive and save, i get an excel crash..
when it does work, the end result is (image attached)
Any help would be appreciated!
I'm attempting to modify the code to where if the first file path is unavailable, then it will save the backup to the second file path.
I think i'm close, but when I disconnect from wifi to detach from the network drive and save, i get an excel crash..
VBA Code:
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
' Location to save Personal.XLSB and backup if necessary
Const primaryPath As String = "\\readyshare\USB_Drive\Codes\Personal.XLSB"
Const backupPath As String = "C:\Users\User1\Documents\PersonalXLSB\Personal.XLSB"
Application.DisplayAlerts = False
' Check if primary path is available
If Dir(primaryPath) = "" Then
' Save to backup path if primary path is available
ThisWorkbook.SaveCopyAs primaryPath & Format(Now(), "_yyyymmdd.bak")
Else
' Save to backup path if primary is not available
ThisWorkbook.SaveAs backupPath & Format(Now(), "_yyyymmdd.bak")
End If
Application.DisplayAlerts = True
End Sub
when it does work, the end result is (image attached)
Any help would be appreciated!