VBA & Macro Problems

Create Unique Excel File Names When Saving with VBA

Steve KSteve K Nov 20, 2026 868 views

Question details

Automatically save an Excel workbook under an incremented file name when the intended name already exists.

Create Unique Excel File Names When Saving with VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

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.

2
Add 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

3
Run the macro

Return to Excel and activate the workbook to save. Select Developer > Macros, choose SaveWithUniqueName, and click Run. Select the destination folder when prompted.

Use VBA to Find an Unused Workbook File Name
Use the matching file format: The example uses .xlsm to preserve VBA macros. For a workbook that does not need macros, you can use .xlsx and change the FileFormat argument to xlOpenXMLWorkbook. The name check and save are separate operations, so another user could create the same file between them on a shared folder.
Free Microsoft Office alternative

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.

Use Save As to give spreadsheet copies distinct names.Open and save Microsoft Excel formats, including .xlsx.Work in a familiar spreadsheet interface for an easier transition.Use a free, lightweight office suite.
microsoft office alternative - wps office

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.