VBA Code to add to a save as file name

chrissmarlow

Board Regular
Joined
Jun 3, 2010
Messages
59
Hi Guys,
I am using the follwoing code on the end of my macro to save a copy of the spreadsheet with todays date, is there a way of adding a "v1" or "v2" to the filename if there is already a file with that name (if someone has already run the macro on that day) rather than just overwritting it? Thanks for the help


Code:
ActiveWorkbook.SaveAs "T:\SupplyChain\Order Book Management\Reports\" & "SRS Sheet - " & Format(Date, "dd-mm-yyyy-") & ".xlsm", FileFormat:=52
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi Chris
Here's some code that demonstrates the principle of how you could do this

Code:
Sub test()
    Dim x As String
    Dim i As Integer
 
    x = ""
    i = 0
    x = Dir("c:\test.xls")
    If x <> "" Then
        Do
            i = i + 1
            x = Dir("c:\test v" & i & ".xls")
        Loop While x <> ""
        'end of versioned files found, save with new version number
        ActiveWorkbook.SaveAs "c:\test v" & i & ".xls"
 
    Else
        'file not found so save as base name
        ActiveWorkbook.SaveAs "c:\test.xls"
 
    End If
End Sub

Hope it make sense

Cheers

Gordon
 
Upvote 0
Hi Chris
Here's some code that demonstrates the principle of how you could do this

Code:
Sub test()
    Dim x As String
    Dim i As Integer
 
    x = ""
    i = 0
    x = Dir("c:\test.xls")
    If x <> "" Then
        Do
            i = i + 1
            x = Dir("c:\test v" & i & ".xls")
        Loop While x <> ""
        'end of versioned files found, save with new version number
        ActiveWorkbook.SaveAs "c:\test v" & i & ".xls"
 
    Else
        'file not found so save as base name
        ActiveWorkbook.SaveAs "c:\test.xls"
 
    End If
End Sub

Hope it make sense

Cheers

Gordon

Thanks very much Gordon, I will give it a go
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,733
Members
452,939
Latest member
WCrawford

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