Results 1 to 7 of 7

Thread: How to save filename with -v2 -v3 and so on at the end if file name already exists

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Regular
    Joined
    Jul 2011
    Posts
    11
    Location

    Question How to save filename with -v2 -v3 and so on at the end if file name already exists

    I have the following code and I want to change it such that:
    1) if the file name C:\abc.xlsx already exists, it will save as C:\abc-v2.xlsx;
    2) if both the file name C:\abc.xlsx and C:\abc-v2.xlsx already exist, it will save as abc-v3.xlsx;
    ...so on and so forth.

    [VBA]Sub copysavefile()
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Sheets(Array("1", "18")).Select
    Sheets("37").Activate
    Sheets(Array("1", "18")).Copy
    Sheets("1").Name = "summary"
    Sheets("18").Name = "main"
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    ActiveWorkbook.SaveAs Filename:= "C:\abc.xlsx" , FileFormat:= _
    xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    MsgBox ("Complete creating " & Range("J2").Value & ".")
    ActiveWindow.Close 'ActiveWorkbook.Close savechanges = True
    End Sub[/VBA]


    Thanks in advance!
    Last edited by Bob Phillips; 07-25-2012 at 12:05 AM. Reason: Added VBA tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •