VBA & Macro Problems

Save an Excel VBA PDF Without Overwriting Existing Files

Jason MillerJason Miller Nov 18, 2026 869 views

Question details

Use Excel VBA to export a worksheet as a PDF with an unused filename.

Save an Excel VBA PDF Without Overwriting Existing Files
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In desktop Excel, press Alt+F11, then select Insert > Module.

2
Add the export macro

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

3
Run the macro

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.

Check for an unused filename before exporting the worksheet
Filename checks are not atomic: The macro checks the name immediately before exporting. If another process creates the same file between the check and the export, the check cannot guarantee that the file will be preserved.
Free Microsoft Office alternative

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.

Export spreadsheets to PDF from WPS Office.Open and edit Microsoft Excel formats such as XLSX and XLS.Use a familiar spreadsheet interface when moving from Microsoft Office.Get a free, lightweight office suite for everyday document work.
microsoft office alternative - wps office

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.