If Directory save

komobu

New Member
Joined
Feb 7, 2011
Messages
37
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

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
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
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,264
Members
449,075
Latest member
staticfluids

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