VBA & Macro Problems

Save an Excel Worksheet as a PDF with a Dynamic Filename and Preserve VBA

Natalie TaylorNatalie Taylor Nov 27, 2026 869 views

Question details

Export a worksheet to a PDF with a filename generated from worksheet values and save the workbook without removing its macros.

Export an Excel Worksheet to PDF with a Dynamic Filename and Preserve VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A VBA macro creates a report filename from worksheet cells and saves the workbook and a PDF copy.
Observed behavior
The workbook appears to be saved in a macro-free format, preventing its VBA macros from working afterward.
Before you start

Save the macro-containing workbook in the folder where you want the output files. Identify the cells containing the report number, customer number, product type, and date; the example below uses B2 through B5.

Solution 1Recommended

Export the worksheet as a PDF and save the workbook as XLSM

Use the same generated base filename for both files, and specify the macro-enabled workbook format when saving.

The example uses the active worksheet in the workbook containing the macro. Adjust the cell references and worksheet selection to match your report.

The code avoids overwriting a different existing XLSM file with the generated name. Check for an existing PDF with that name before running it, as the export may replace it.

1
Open the VBA editor

In Excel, open the macro-containing workbook, press Alt+F11, and choose Insert > Module. Place the macro in this workbook so ThisWorkbook refers to the file you want to save.

2
Add the export macro

Paste the following code into the module. Change B2, B3, B4, and B5 if your report values are stored elsewhere: Sub SaveReportAndPDF() Dim wb As Workbook Dim ws As Worksheet Dim folder As String, baseName As String Dim xlsmPath As String, pdfPath As String Dim badChars As Variant, ch As Variant Set wb = ThisWorkbook Set ws = wb.ActiveSheet If wb.Path = "" Then MsgBox "Save the workbook in the output folder first." Exit Sub End If If Not IsDate(ws.Range("B5").Value) Then MsgBox "Enter a valid date in B5." Exit Sub End If folder = wb.Path & Application.PathSeparator baseName = Trim(CStr(ws.Range("B2").Value)) & "_" & _ Trim(CStr(ws.Range("B3").Value)) & "_" & _ Trim(CStr(ws.Range("B4").Value)) & "_" & _ Format(CDate(ws.Range("B5").Value), "yyyy-mm-dd") badChars = Array("\", "/", ":", "*", "?", Chr(34), "<", ">", "|") For Each ch In badChars baseName = Replace(baseName, CStr(ch), "-") Next ch xlsmPath = folder & baseName & ".xlsm" pdfPath = folder & baseName & ".pdf" If StrComp(wb.FullName, xlsmPath, vbTextCompare) = 0 Then wb.Save Else If Dir(xlsmPath) <> "" Then MsgBox "A workbook with that name already exists: " & xlsmPath Exit Sub End If wb.SaveAs Filename:=xlsmPath, FileFormat:=52 End If ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath MsgBox "Saved workbook and PDF in: " & folder End Sub

3
Run the macro

Return to Excel, select the report worksheet, and use Developer > Macros to run SaveReportAndPDF. Check the workbook’s folder for the XLSM and PDF files.

Export the worksheet as a PDF and save the workbook as XLSM
Why the format matters: FileFormat 52 saves an Excel Macro-Enabled Workbook. Saving as XLSX does not preserve VBA code, even if you change only the filename extension.
Free Microsoft Office alternative

Work with Excel reports in WPS Spreadsheet

WPS Office is a free, lightweight Office alternative with a familiar interface for opening and editing spreadsheet reports. Its Excel format compatibility helps you move existing files into your workflow; check VBA support in your WPS edition before relying on Excel macros.

Open and edit report spreadsheets in a familiar interfaceCompatible with Microsoft Excel formats including XLSX and XLSMExport spreadsheets to PDF for sharingMove existing Office documents into a lightweight workspace
microsoft office alternative - wps office

Frequently Asked Questions

Why do my macros disappear when I save the workbook?

The XLSX format cannot retain VBA code. In Excel, choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm), or use FileFormat 52 in VBA.

How do I make the PDF and workbook filenames match?

Build one baseName string from the report cells, then append .xlsm for the workbook path and .pdf for the ExportAsFixedFormat filename.

How can I export a specific worksheet instead of the active one?

Replace Set ws = wb.ActiveSheet with a named reference such as Set ws = wb.Worksheets("Report"), using the actual worksheet name.

Where are the files saved by the example macro?

They are saved in ThisWorkbook.Path, the folder containing the macro workbook. Save the workbook to the desired folder before running the macro.