If FILENAME exists in directory, save as FILENAME1 or FILENAMEx etc.

Gingertrees

Well-known Member
Joined
Sep 21, 2009
Messages
697
Looking for a way to automatically add a check digit (or whatever) to the end of a file name, if the filename intended already exists in the target directory. Here's my code, which works brilliantly if the filename isn't taken:
Code:
Sub FinalProcessing()
'////Sub 8//////'////Sub 8//////'////Sub 8//////

    Set ProjectNm = Activesheet.Range("F1")
     
      '//this next section copies the processed orange report into a new file
      '//New file can be emailed or uploaded to Sharepoint

Dim SourceWb As Workbook
Dim DestinationWb As Workbook
Dim Datestring As String
 
    Set SourceWb = ThisWorkbook
    Datestring = Format(Now, "mm-dd-yy")
 
    SourceWb.ActiveSheet.Copy
    Set DestinationWb = ActiveWorkbook
    With DestinationWb
        .SaveAs SourceWb.Path & "\Orange " & ProjectNm & " " & Datestring & " USER.xlsx"
 '/-----------------
 '/add code here to handle if filename
 '/already exists, giving this a title of 
 '/e.g. "Orange projectname 09-08-15 USER1.xlsx"
 '/-----------------
        .Close
    End With
      '//just the orange sheet has now been saved
      '//to the same directory location as this workbk.
      '//It's called "Orange projectname today's date-time.xslx"
      '//
Application.ScreenUpdating = True
Application.DisplayAlerts = True
 
If MsgBox("And there was much rejoicing.", vbOKOnly) = vbOK Then Exit Sub
 
End Sub
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,223,912
Messages
6,175,348
Members
452,638
Latest member
Oluwabukunmi

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