How to Create One PDF from Multiple Excel Reports Using VBA
Question details
The user wants to generate multiple variations of a report by changing a specific cell value (e.g., cost center) and compile all the resulting reports into a single, multi-page PDF file using a VBA macro.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating the generation and consolidation of dynamic expense reports for different cost centers into one continuous PDF document.
- Observed behavior
- Currently, the user has to create separate PDF files for each cost center report, but the goal is to output a single combined PDF using a VBA loop.
Ensure that your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have a clearly defined list of the variable values (e.g., cost center codes) to loop through.
Use a VBA Loop to Export Sheets as a Single PDF
Write a VBA macro that loops through a list of values, updates the target cell, temporarily copies the generated report to a new sheet, and exports all collected sheets as one PDF.
Because Excel's native ExportAsFixedFormat method exports the active sheet or selected sheets, you cannot append pages to an existing PDF directly through standard VBA. The most reliable workaround is to loop through your data, generate each report, copy the result to a temporary sheet, and then export all temporary sheets together as a single PDF.
Create a named range or a specific column containing the list of values (e.g., cost centers) you want to iterate through.
Press Alt + F11 to open the Visual Basic for Applications editor, then insert a new Module by clicking Insert > Module.
Create a 'For Each' loop in your script to cycle through your list of values. Inside the loop, assign the current value to the target cell (for example, Range("B2").Value = cell.Value) so the report recalculates.
Still inside the loop, instruct VBA to copy the recalculated report range or worksheet to the end of the workbook as a new temporary sheet. Keep track of the names of these newly created sheets.
After the loop finishes, select all the newly created temporary sheets simultaneously using an array. Call the ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF command to generate the single PDF, and finally, delete the temporary sheets to keep your workbook clean.

Automate and Export Reports to PDF in WPS Spreadsheet
WPS Office offers robust support for VBA macros, allowing you to seamlessly run your Excel automation scripts. Easily loop through data, generate dynamic reports, and export them into a single PDF without compatibility issues.
- 1. Open your Workbook: Launch WPS Spreadsheet and open your Macro-Enabled Workbook (.xlsm).
- 2. Access the VBA Editor: Navigate to the 'Developer' tab and click on 'Macros' or 'Visual Basic' to access the VBA Editor.
- 3. Run your Macro: Paste your PDF consolidation macro into a new module and click 'Run'.
- 4. View your Combined PDF: WPS Spreadsheet will execute the loop, generate the temporary sheets, and seamlessly export the combined PDF to your designated folder.

Frequently Asked Questions
Can I append pages to an existing PDF directly using Excel VBA?
No, Excel's native VBA ExportAsFixedFormat method cannot append pages to an already created PDF. You must gather all data onto multiple sheets or into one large sheet before exporting, or use a third-party PDF tool's API (like Adobe Acrobat) within your VBA script.
Why is my VBA macro creating separate PDFs instead of one?
This usually happens if the ExportAsFixedFormat command is placed inside the loop that changes the cell value. To create one PDF, you must run the export command only once on a selection of multiple sheets after the loop has fully finished generating all the individual reports.
How do I select multiple sheets in VBA to export as one PDF?
You can select multiple sheets by passing an array of sheet names to the Select method, like Sheets(Array("Report1", "Report2")).Select, and then executing the export command on the ActiveSheet.




