If Directory save

komobu

New Member
Joined
Feb 7, 2011
Messages
36
I am using the following code to save back up files:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Saves the current file to a backup folder and the default folder
'Note that any backup is overwritten
Application.DisplayAlerts = False

ActiveWorkbook.SaveCopyAs ("C:\Users\User\Dropbox\Documents\Files and Rentals " & Format(Now(), "DD MMM YYYY hhmmss") & ".xlsm")
ActiveWorkbook.SaveCopyAs ("C:\Users\User\OneDrive\Rental Stuff\Files and Rentals " & Format(Now(), "DD MMM YYYY hhmmss") & ".xlsm")
ActiveWorkbook.SaveCopyAs ("Y:\Excel BackUp Docs\Files and Rentals " & Format(Now(), "DD MMM YYYY hhmmss") & ".xlsm")

ActiveWorkbook.Save
Application.DisplayAlerts = True
End Sub

My question comes in my third ActiveWorkBook.SaveCopyAs statement. I use this workbook from two different locations. At home, I have the "Y:\" directory. At my work location, that directory doesnt exist. So what I am trying to do is code it that if the directory exists (I am at home), it will save a copy there, and if the directory doesnt exist (I am at work), it will just pass over it and go to the next line of ActiveWorkBook.Save.

Any help on coding this would be appreciated.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
60,197
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
If Dir("Y:\Excel BackUp Docs\", vbDirectory) <> "" Then
   ActiveWorkbook.SaveCopyAs ("Y:\Excel BackUp Docs\Files and Rentals " & Format(Now(), "DD MMM YYYY hhmmss") & ".xlsm")
End If
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
60,197
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Forum statistics

Threads
1,136,909
Messages
5,678,522
Members
419,769
Latest member
Sparks66

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
Top