Export Multiple Excel Data Sets to PDF Using VBA Without Overwriting
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.

- 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.
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.
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.
Press Alt + F11 on your keyboard to launch the VBA Editor, then locate the module containing your export macro.
Find the line of code that executes the PDF export, which typically begins with ActiveSheet.ExportAsFixedFormat.
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"
Save your code and run the macro. The script will now generate sequentially numbered files such as test1.pdf, test2.pdf, and so on.

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. Open your Macro-Enabled File: Launch WPS Spreadsheet and open your existing .xlsm file containing the data sets.
- 2. Access the VBA Editor: Navigate to the 'Tools' tab on the ribbon and click on 'Macro' to open the visual basic editor.
- 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. 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.

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.




