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

Surii

New Member
Joined
Jul 14, 2011
Messages
9
Hi all,
I have the following code (to copy sheets to a new workbook and save) 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.

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

ActiveWindow.Close 'ActiveWorkbook.Close savechanges = True

End Sub


Thanks in advance for your help! :)
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Code:
Sub SaveAs_NewVersion()
    
    Dim fso             As Object
    Dim strFilename     As String
    Dim strPath         As String
    Dim lngCount        As Long
    
    strPath = "C\:"
    strFilename = ThisWorkbook.Name
    
    Set fso = CreateObject("scripting.filesystemobject")
    
    While fso.FileExists(strPath & strFilename) = True
        lngCount = lngCount + 1
        If Mid(strFilename, InStr(strFilename, ".") - 2, 1) = "V" And IsNumeric(Mid(strFilename, InStr(strFilename, ".") - 1, 1)) Then
            lngCount = CLng(Mid(strFilename, InStr(strFilename, ".") - 1, 1)) + 1
            strFilename = Mid(strFilename, 1, InStr(strFilename, ".") - 2) & lngCount & "." & _
                             Right(strFilename, Len(strFilename) - InStr(1, strFilename, "."))
        Else
           strFilename = Mid(strFilename, 1, InStr(strFilename, ".") - 1) & "_V" & lngCount + 1 & "." & _
                           Right(strFilename, Len(strFilename) - InStr(1, strFilename, "."))
       End If
    Wend
    
    ThisWorkbook.SaveAs Filename:=strPath & strFilename, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    
    Set fso = Nothing
    strFilename = vbNullString
    strPath = vbNullString
    lngCount = Empty
    
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,365
Messages
6,124,512
Members
449,167
Latest member
jrob72684

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