VBA & Macro Problems

How to Export Multiple Excel Worksheets to Separate PDFs with VBA

Steve KSteve K Oct 23, 2026 869 views

Question details

Automate the creation of individual PDF files from specific Excel worksheets for a large batch of employees.

How to Export Multiple Excel Worksheets to Separate PDFs with VBA
Product
Excel
Device & OS
not provided
Scenario
Generating individual PDF reports for over 200 employees by selecting specific worksheets and dynamically naming the files based on employee IDs and names.
Observed behavior
Requires a VBA macro to loop through a dataset, update reference cells, group specific sheets, and export them directly to a designated folder as PDFs.
Before you start

Verify that the destination folder for your PDFs already exists on your computer, as the VBA script will throw an error if the directory is missing. It is also recommended to save a backup copy of your workbook before running any macro.

Solution 1Recommended

Create a VBA Macro to Loop and Export Specific Worksheets

Use a VBA loop to iterate through employee records, select the necessary sheets, and export them as PDFs using the ExportAsFixedFormat method.

This method uses a macro loop to process each employee sequentially. It updates a designated cell to reflect the current employee, selects a specific group of sheets, and exports them as a single PDF.

The filename is dynamically generated during each loop iteration, combining the folder path with the employee ID and name to ensure every file is uniquely identifiable.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic for Applications (VBA) editor in Excel.

2
Insert a New Module

Click on 'Insert' in the top menu bar, then select 'Module' to open a blank script window.

3
Write the Loop Code

Set up a For loop to iterate through your list of employees. Inside the loop, assign the current Employee ID to your designated data reference cell so the worksheets update with the correct employee's data.

4
Select Worksheets and Export

Group the required sheets using a command like Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select. Next, use ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=yourDynamicFilePath to export them.

5
Execute the Macro

Press F5 to run the macro. Check your target destination folder to verify that all individual PDF files have been generated correctly.

Create a VBA Macro to Loop and Export Specific Worksheets
Check Folder Path Formatting: Ensure you declare all variables properly in your script and that your base filepath string ends with a backslash (e.g., 'C:\Employee Reports\').

Automate PDF Exports Seamlessly with WPS Office

WPS Office fully supports VBA and macros, allowing you to seamlessly run your Excel scripts to export multiple worksheets to PDF. Its advanced Spreadsheet tool handles large datasets and complex automations with ease, ensuring high performance even with loops processing hundreds of records.

  1. 1. Open Your Workbook in WPS Spreadsheets: Launch WPS Office and open your .xlsm file containing the employee data and macro.
  2. 2. Enable Macro Support: Navigate to the 'Developer' tab in the ribbon menu and ensure Macros are enabled for the current workbook.
  3. 3. Run the Export Macro: Click 'Macros', select your PDF export script from the list, and click 'Run' to instantly generate all your employee PDF files.
Fully compatible with Microsoft Excel VBA macros (.xlsm)Built-in PDF conversion and high-quality document export featuresLightweight architecture handles large automated data loops smoothlyFree to download and use for advanced daily office tasks
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get a 'Run-time error 1004' when exporting to PDF via VBA?

This error most commonly occurs if the destination folder specified in your VBA filename string does not exist, or if you do not have permission to write files to that directory. Double-check your file path string and create the folder before running the macro.

How do I ensure only specific sheets are exported into the PDF?

You can explicitly select the desired sheets by using the Array function in VBA, for instance: Sheets(Array("Sheet1", "Sheet3")).Select. Once selected, only those active sheets will be processed by the ExportAsFixedFormat command.

Can I name the exported PDFs dynamically using cell values?

Yes. In your VBA script, you can concatenate a folder path string with specific cell values. For example: Filename:="C:\Folder\" & Range("A2").Value & "_" & Range("B2").Value & ".pdf".

Does this VBA macro for exporting PDFs work in WPS Office?

Yes, WPS Office Spreadsheets has excellent VBA support. You can activate the Developer tab, open the visual basic editor, and run your ExportAsFixedFormat scripts to generate PDFs without needing to alter your original Excel code.