VBA - add number at the end if file exists

Yulyo

Board Regular
Joined
Jul 17, 2017
Messages
94
Hello all,

Short question from me if you have a few seconds:
- I have the following VBA that saves the active sheet in a folder on the desktop

VBA Code:
 With Application.FileDialog(msoFileDialogSaveAs)
        .InitialFileName = Path & "\" & Format(Range("K2"), "YYMMDD") & "_" & Range("L12") & "_(" & NM & " )" & "_" & Range("P3") & ".xlsx"
        .Show
        .Execute
 End With

- the problem that I am facing is that if I try to save the file again (it might happened that the file saved by other colleagues have the same name), it tells me that the file already exists. I am trying to change this code and make it add a "-1", "-2"... at the end, if file exist.
So if the initial name of the file is test.xlsx and I try to save the file again with the same name, the new file should be named test-1.xlsx, if I save it again the name should be named test-2.xlsx and so on.

Thank you
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Here is the code I use to check for existing files with the same name. You could change the name of the variable RptPath


VBA Code:
RptPath = RptPath & " " & Format(Now(), "YYYYMMDD") & "_"
  For X = 1 To 20
    S = RptPath & X & ".xlsx"
    A = Dir(S)
    If A = "" Then
      RptPath = S
      Exit For
    End If
  Next X
 
Upvote 0
Hi @Yulyo. Thanks for posting on the forum.

Try the following. Fit the missing data in the nm and sPath variables

VBA Code:
Sub savesheet()
  Dim sPath As String, nm As String, prefix As String, sExt As String, NewName As String
  Dim n As Long
  
  nm = "test"                 'Here the value of the variable nm
  sPath = "c:\desktop"        'Here your intial path
  
  prefix = sPath & "\" & Format(Range("K2"), "YYMMDD") & "_" & Range("L12") & "_(" & nm & " )" & "_" & Range("P3")
  sExt = ".xlsx"
  NewName = prefix & sExt
  Do While True
    If Dir(NewName) = "" Then Exit Do
    n = n + 1
    NewName = prefix & "-" & n & sExt
  Loop
  
  With Application.FileDialog(msoFileDialogSaveAs)
    .InitialFileName = NewName
    .Show
    .Execute
  End With
End Sub

--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------
 
Upvote 0
Solution

Forum statistics

Threads
1,215,137
Messages
6,123,252
Members
449,093
Latest member
Vincent Khandagale

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