Save an Excel VBA PDF Without Overwriting Existing Files
Question details
Use Excel VBA to export a worksheet as a PDF with an unused filename.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- A user wants to save repeated PDF exports in the same folder while retaining earlier copies.
- Observed behavior
- The desired export checks for existing filenames and adds a numbered suffix when needed.
In desktop Excel for Windows, save the workbook as a macro-enabled .xlsm file in a local folder and enable macros if you trust the workbook. The example uses that folder and exports the active worksheet.
Check for an unused filename before exporting the worksheet
Use Dir to find an available PDF name, adding numbered suffixes until there is no matching file.
In desktop Excel, press Alt+F11, then select Insert > Module.
Paste this code into the module. Change "Report" to the base filename you want: Sub SavePDFWithoutOverwrite() Dim folderPath As String Dim baseName As String Dim pdfPath As String Dim suffix As Long folderPath = ThisWorkbook.Path If folderPath = "" Then MsgBox "Save the workbook to a local folder first." Exit Sub End If baseName = "Report" pdfPath = folderPath & Application.PathSeparator & baseName & ".pdf" suffix = 1 Do While Dir(pdfPath, vbNormal Or vbHidden Or vbSystem Or vbReadOnly Or vbArchive) <> "" pdfPath = folderPath & Application.PathSeparator & baseName & " (" & suffix & ").pdf" suffix = suffix + 1 Loop ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath MsgBox "PDF saved to: " & pdfPath End Sub
Return to Excel, select the worksheet to export, press Alt+F8, select SavePDFWithoutOverwrite, and click Run. Check the workbook's folder for the new PDF.

Work with spreadsheets and export PDFs in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for opening spreadsheets and exporting them to PDF. Its Microsoft Office format support helps you move existing files into WPS; it does not fix Excel VBA behavior, and macro compatibility depends on the WPS version.

Frequently Asked Questions
What happens if Report.pdf and Report (1).pdf already exist?
The macro keeps checking numbered names and exports to the first unused one, such as Report (2).pdf.
How do I change the PDF filename or save folder?
In the VBA module, change the baseName value to your preferred filename. The example saves to ThisWorkbook.Path; replace that assignment with an existing local folder path if you want a different destination.
Why does the macro ask me to save the workbook first?
ThisWorkbook.Path is empty when the workbook has not been saved. Use File > Save As to save it as an .xlsm file in a local folder, then run the macro again.
Does the macro export the whole workbook?
No. ActiveSheet.ExportAsFixedFormat exports the currently selected worksheet. Select the intended worksheet before running the macro.




