VBA Save Personal.XLSB to second file path if first file path is not available

Knockoutpie

Board Regular
Joined
Sep 10, 2018
Messages
116
Office Version
  1. 365
Platform
  1. 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..

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!
 

Attachments

  • Screenshot 2024-01-08 110916.png
    Screenshot 2024-01-08 110916.png
    4.5 KB · Views: 10

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Have a try with this added code:
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
    On Error GoTo primaryNA                       '<-added
    ' 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
primaryNA:                                        '<-added
        On Error GoTo 0                           '<-added
        ' Save to backup path if primary is not available
        ThisWorkbook.SaveAs backupPath & Format(Now(), "_yyyymmdd.bak")
    End If
    Application.DisplayAlerts = True
End Sub
 
Upvote 1
Solution
Hey, just had to move the on error GoTo part a little higher up and we fixed the issue, works as proper now!
IF network drive is not available, backup personal.XSLB to local directory. Perfect for when i'm on the go or in the office. Thanks a bunch!

VBA Code:
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
On Error GoTo primaryNA
    ' Location to save Personal.XLSB and backup if necessary
   Const primaryPath As String = "\\readyshare\Network_Drive\Code_Backups\Personal.XLSB"
   Const backupPath As String = "C:\Users\USER1\Documents\PersonalXLSB\Personal.XLSB"
    Application.DisplayAlerts = False
                         '<-added
    ' 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
primaryNA:                                        '<-added
        On Error GoTo 0                           '<-added
        ' Save to backup path if primary is not available
        ThisWorkbook.SaveAs backupPath & Format(Now(), "_yyyymmdd.bak")
    End If
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
just had to move the on error GoTo part a little higher up and we fixed the issue
Sure ?:unsure: the line that would throw an error is: ThisWorkbook.SaveCopyAs primaryPath & Format(Now(), "_yyyymmdd.bak") so the On Error line could have been placed just before it; but if you had an issue it's okay for me too;).
Thanks for the feedback(y), glad having been of some help.
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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