VB CODE to save file unique name

ateebali

Board Regular
Joined
Dec 13, 2018
Messages
108
I am using following vb code to save file with cell range;
Dim Path As StringDim filename As String
Path = ThisWorkbook.Path & "\Backup" & ""
filename = Range("O6")
ActiveWorkbook.SaveAs filename:=Path & filename & ".xlsb", FileFormat:=50
Application.DisplayAlerts = False

I need to amend it that if file already save with this name, it should save the file with extension for example, if file name ABC.xlsb already there, it saves as ABC (1)
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Please sir, requesting again for help, if file already exist in folder, it should save as new name, I need help please
This code I am using to save file, how to amend it so it worked the way;
Dim Path As StringDim fileName As String
If Dir(ThisWorkbook.Path & "\Backup", vbDirectory) = vbNullString Then MkDir ThisWorkbook.Path & "\Backup"
Path = ThisWorkbook.Path & "\Backup" & ""


fileName = Range("O6")
ActiveWorkbook.SaveAs fileName:=Path & fileName & ".xlsb", FileFormat:=50
Application.DisplayAlerts = False
 
Upvote 0
I have tried this but its not working;
Dim Path As StringDim fileName As String
If Dir(ThisWorkbook.Path & "\Backup", vbDirectory) = vbNullString Then MkDir ThisWorkbook.Path & "\Backup"
Path = ThisWorkbook.Path & "\Backup" & ""


fileName = Range("O6")
ActiveWorkbook.SaveAs fileName:=Path & fileName & ".xlsb", FileFormat:=50
Application.DisplayAlerts = False


End Sub


Function GeneratedFileName(fileName As String, i As Integer)
Dim testFileName As String
testFileName = fileName
If i <> 0 Then
testFileName = fileName & " (" & i & ")"
End If
GeneratedFileName = testFileName
If Dir(testFileName & ".xlsb", vbNormal) > "" Then
GeneratedFileName = GeneratedFileName(fileName, i + 1)
End If


End Function
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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