saving to multiple locations/check for existing file/create extension

lakke2120

New Member
Joined
Aug 21, 2014
Messages
32
Hello,
I have a code that works well for saving to multiple locations. However, I would like for the code to check for existing files in BOTH locations and if one exist then to save under sme name with different version. i.e testfilev1. Hope I explained that right.

thank you very much
___________________________________________________________________________________
Private Sub Workbook_BeforeClose(Cancel As Boolean)

If MsgBox("Would you like to back up file?", vbYesNo) = vbYes Then _

Dim newFile As String, fName As String
' Don't use "/" in date, invalid syntax
fName = Sheets("Service Objective").Range("AH1").Value
'Change the date format to whatever you'd like, but make sure it's in quotes
newFile = fName & " " & Format$(Date, "mmmm-yyyy")
' Change directory to suit your PC, including USER NAME
ChDir _
"S:\Active Individual Programs\Manager folder\service objectives\"
ActiveWorkbook.SaveAs Filename:=newFile
ActiveWorkbook.SaveAs "S:\Active Individual Programs\All Individual SAP Programs\1 Master template\2015\Data\" + ActiveWorkbook.Name
End If
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
This adds a check digit to a file name if the name would already exist in the target location. :
Rich (BB code):
Dim n as integer
n=0
Do
    newFile = ChDir "S:\Active Individual Programs\Manager folder\service objectives\" & fName _ 
    & " " & Format$(Date, "mmmm-yyyy") & _
    IIf(n=0, "", n) & ".xlsx"
    n = n+1
Loop until Dir(newFile) = ""

ActiveWorkbook.SaveAs Filename:=newFile
'//this next part might not be necessary:
'////ActiveWorkbook.SaveAs "S:\Active Individual Programs\All Individual SAP Programs\1 Master template\2015\Data\" & ActiveWorkbook.Name
 
Upvote 0
Private Sub Workbook_BeforeClose(Cancel As Boolean)

If MsgBox("Would you like to back up file?", vbYesNo) = vbYes Then _

Dim n As Integer
n = 0
Do
newFile = ChDir "S:\Active Individual Programs\Manager folder\service objectives\" & fName _
& " " & Format$(Date, "mmmm-yyyy") & _
IIf(n=0, "", n) & ".xlsx"
n = n + 1
Loop Until Dir(newFile) = ""


ActiveWorkbook.SaveAs Filename:=newFile
'//this next part might not be necessary:
'////ActiveWorkbook.SaveAs "S:\Active Individual Programs\All Individual SAP Programs\1 Master template\2015\Data\" & ActiveWorkbook.Name
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,334
Messages
6,124,319
Members
449,154
Latest member
pollardxlsm

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