VBA & Macro Problems

How to Export Multiple Excel Worksheets to One Password-Protected PDF with VBA

Nimra MalikNimra Malik Oct 23, 2026 868 views

Question details

The user wants to select specific worksheets from an Index sheet using VBA, export them combined into a single PDF file, save the file in a dynamically created date-based folder, and add password protection to the PDF.

How to Export Multiple Excel Worksheets to a Single Password-Protected PDF with VBA
Product
Excel
Device & OS
not provided
Scenario
Automating the generation of consolidated daily reports from multiple worksheets and securely storing them as encrypted PDFs.
Observed behavior
The current VBA code exports worksheets as separate PDFs rather than a single document, throws an error when trying to generate date-based file names using Today(), and lacks a method for password-protecting the exported PDF file.
Before you start

Ensure you have saved your Excel workbook as a Macro-Enabled Workbook (.xlsm) and that your target directory allows folder creation permissions before running the VBA script.

Solution 1Recommended

Export Combined Worksheets to PDF and Fix Date Formatting

Use a VBA array to select multiple sheets at once so they export as a single PDF, and correct the invalid date function for dynamic folder naming.

To prevent Excel from generating separate PDFs for each worksheet, you must select all target sheets simultaneously before executing the export command. This is achieved by storing the worksheet names in an array.

Additionally, utilizing the correct VBA Date function ensures your directory paths are built without errors.

1
Fix the date function error

In your VBA code, locate the line causing the error: Filename = Format(Today(), "mmddyyyy"). Replace Today() with Date, which is the correct VBA function, resulting in Filename = Format(Date, "mmddyyyy").

2
Build an array of worksheet names

Read the list of target sheet names from your 'Index' worksheet and populate them into a variant array, or manually define them using Sheets(Array("Sheet1", "Sheet2")).

3
Select the worksheets simultaneously

Group the sheets by selecting the array: ActiveWorkbook.Worksheets(yourArray).Select. This ensures Excel treats them as a single print job.

4
Define the dynamic folder path

Build your save directory using ThisWorkbook.Path & "\Export\" & Format(Date, "mmddyyyy") & "\". Ensure you use the MkDir function in VBA to create this folder if it does not already exist.

5
Execute ExportAsFixedFormat

Call ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=yourPath & "Report.pdf" to generate a single merged PDF file containing all selected sheets.

Export Combined Worksheets to PDF and Fix Date Formatting
Password Protection Limitation in Excel VBA: Excel VBA's native ExportAsFixedFormat method cannot add passwords or encryption to PDFs. To password-protect the file, you must either use an external command-line PDF tool triggered via VBA Shell, or use alternative office software that supports native PDF encryption.

Easily Combine Sheets and Encrypt PDFs with WPS Office

Instead of relying on complex VBA arrays and third-party tools to handle PDF passwords, WPS Office Spreadsheet allows you to natively select multiple sheets, combine them into one PDF, and apply password encryption directly from the user interface.

  1. 1. Select target worksheets: Hold down the Ctrl key on your keyboard and click the tabs of the specific worksheets you want to combine.
  2. 2. Access the PDF export tool: Go to the Menu tab in WPS Spreadsheet and select 'Export to PDF' from the dropdown list.
  3. 3. Choose export range: In the export dialog, ensure the export range is set to 'Active sheet(s)' so only your selected tabs are included in the final document.
  4. 4. Apply password encryption: Click the 'Advanced' or 'Encrypt' option within the export window, check the box to set a password, enter your desired security password, and confirm.
  5. 5. Export and save: Select your destination folder and click the 'Export' button to instantly generate a single, password-protected PDF.
Combine multiple worksheets into a single PDF seamlessly without macros.Natively add open passwords and permissions to PDFs upon export.Fully compatible with Microsoft Excel (.xlsx and .xlsm) file formats.Free and lightweight software featuring an intuitive interface.
microsoft office alternative - wps office

Frequently Asked Questions

Why does the Format(Today(), "mmddyyyy") code trigger an error in VBA?

While Today() is a valid formula function inside Excel cells, it is not recognized as a native command in VBA. You must use the Date function instead to retrieve the current system date in your macro.

Can I add a password to my PDF using Excel VBA's ExportAsFixedFormat?

No, Microsoft Excel's native VBA method for exporting PDFs does not support security parameters. You would need to use a third-party application, Adobe Acrobat's API, or alternative software like WPS Office which has built-in PDF encryption features.

How do I group sheets dynamically based on a list in my workbook?

You can use a VBA Loop (For Each or For/Next) to iterate through a range of cells containing your worksheet names, add those string values to a dynamically sized Array, and finally pass that array to Worksheets(myArray).Select to group them before exporting.