Create Unique Excel File Names When Saving with VBA
Question details
Automatically save an Excel workbook under an incremented file name when the intended name already exists.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A user wants a VBA macro to save duplicate workbooks without overwriting existing files.
- Observed behavior
- The macro should check for an existing file and append a number before the extension until it finds an unused name.
Open the workbook you want to save and make sure macros are enabled. Choose a destination folder and a base file name; the example below uses Report.xlsm.
Use VBA to Find an Unused Workbook File Name
Check the destination with Dir and add a counter before saving the workbook.
This example saves the active workbook as a macro-enabled .xlsm file. It tries Report.xlsm first, followed by Report(1).xlsm, Report(2).xlsm, and so on.
In Excel, open the Developer tab and select Visual Basic. In the editor, choose Insert > Module in a workbook where you can store the macro.
Paste this code into the module. Change baseName if you want a different starting name: Sub SaveWithUniqueName() Dim wb As Workbook Dim folder As String Dim baseName As String Dim extension As String Dim filePath As String Dim counter As Long Set wb = ActiveWorkbook With Application.FileDialog(msoFileDialogFolderPicker) .Title = "Choose a destination folder" If .Show <> -1 Then Exit Sub folder = .SelectedItems(1) End With If Right(folder, 1) <> Application.PathSeparator Then folder = folder & Application.PathSeparator End If baseName = "Report" extension = ".xlsm" filePath = folder & baseName & extension counter = 1 Do While Dir(filePath, vbNormal Or vbHidden Or vbSystem Or vbReadOnly Or vbDirectory) <> "" filePath = folder & baseName & "(" & counter & ")" & extension counter = counter + 1 Loop wb.SaveAs Filename:=filePath, FileFormat:=xlOpenXMLWorkbookMacroEnabled End Sub
Return to Excel and activate the workbook to save. Select Developer > Macros, choose SaveWithUniqueName, and click Run. Select the destination folder when prompted.

Save Spreadsheet Copies with WPS Office
WPS Office is a free, lightweight alternative for working with spreadsheets in a familiar interface. Open an Excel workbook in WPS Spreadsheet and use Save As to create a separately named copy. WPS does not fix Excel's VBA behavior.

Frequently Asked Questions
What happens if Report.xlsm already exists?
The macro checks Report(1).xlsm next and keeps increasing the number until Dir finds an unused name.
How do I change the starting file name?
In the VBA editor, change the value of baseName in the macro, for example from "Report" to "Sales Report".
Can I save the copies as .xlsx files instead?
Yes. Change extension to ".xlsx" and use FileFormat:=xlOpenXMLWorkbook in the SaveAs statement. The .xlsx format does not preserve VBA macros.
Why does the macro ask me to choose a folder?
The folder picker lets you choose the destination each time. The macro checks for existing names in that folder before saving the active workbook.




