VBA & Macro Problems

Export Multiple Excel Data Sets to PDF Using VBA Without Overwriting

Tauseeq MagsiTauseeq Magsi Oct 21, 2026 870 views

Question details

The user needs to export multiple data sets into separate PDF files using a VBA loop without overwriting the previously generated PDF in each iteration.

Export Multiple Excel Data Sets to PDF Using VBA Without Overwriting
Product
Excel
Device & OS
not provided
Scenario
Running a VBA macro that loops through a data range, updates worksheet values, and exports the sheet as a PDF file during each loop iteration.
Observed behavior
Because the VBA code uses a static filename for the PDF export, each loop iteration overwrites the previous file, resulting in only the final data set being saved.
Before you start

Ensure your file is saved as a Macro-Enabled Workbook (.xlsm) and verify that you have full write permissions for the destination folder where the PDFs will be saved.

Solution 1Recommended

Append a Unique Variable to the PDF Filename in VBA

Modify the VBA macro to include the loop counter variable in the output filename, ensuring every exported PDF is given a unique name and preventing overwrites.

When using a loop to generate files in VBA, a hardcoded filename string will constantly overwrite the existing file from the previous iteration. By concatenating the loop counter variable (or another unique identifier) with the base filename, each iteration will create a distinct PDF file.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to launch the VBA Editor, then locate the module containing your export macro.

2
Locate the Export Command

Find the line of code that executes the PDF export, which typically begins with ActiveSheet.ExportAsFixedFormat.

3
Modify the Filename Parameter

Update the Filename parameter to concatenate your loop variable. For example, if your loop variable is 'x', change the filename string to: Filename:=ThisWorkbook.Path & "\test" & x & ".pdf"

4
Run the Updated Macro

Save your code and run the macro. The script will now generate sequentially numbered files such as test1.pdf, test2.pdf, and so on.

Append a Unique Variable to the PDF Filename in VBA
Pro Tip: Instead of using the loop number, you can also name the PDFs dynamically based on cell values. For example, use Sheets("Sheet2").Range("T" & x).Value to name each file after a specific data entry.
Powerful Macro Support

Automate PDF Exports with WPS Spreadsheet

WPS Office provides excellent support for VBA macros and advanced automation. You can easily run your existing loops and export operations seamlessly using WPS Spreadsheet.

  1. 1. Open your Macro-Enabled File: Launch WPS Spreadsheet and open your existing .xlsm file containing the data sets.
  2. 2. Access the VBA Editor: Navigate to the 'Tools' tab on the ribbon and click on 'Macro' to open the visual basic editor.
  3. 3. Insert the Dynamic File Path Code: Paste your updated loop script, ensuring the PDF export path includes the dynamic identifier (like the loop counter).
  4. 4. Execute the Code: Click 'Run' to execute the macro. WPS Spreadsheet will swiftly loop through your data and export all unique PDF files to your target folder.
Fully compatible with Microsoft Excel VBA macros and .xlsm formatsLightweight architecture for fast execution of complex data loopsBuilt-in PDF conversion features for high-quality file exportsFree to use with a familiar, easy-to-navigate interface
microsoft office alternative - wps office

Frequently Asked Questions

Can I combine these separate PDFs into a single file after the VBA export?

Yes. While the VBA macro generates individual PDFs for each data set, you can use PDF editing software such as WPS PDF to batch merge the resulting documents into one cohesive file.

Why am I getting a run-time error when the VBA code tries to save the PDF?

This commonly happens if the target directory does not exist, if the filename contains invalid characters (like slashes or asterisks), or if you lack write permissions. Double-check your ThisWorkbook.Path output and ensure the syntax is correct.

How do I format the loop variable to have leading zeros in the PDF filename?

You can use the VBA Format function. Instead of appending just 'x', use Format(x, "000"). Your code will look like: Filename:=ThisWorkbook.Path & "\test_" & Format(x, "000") & ".pdf", which outputs test_001.pdf, test_002.pdf, etc.