How to Save Excel Data Validation Selections as PDFs Using VBA
Question details
The user needs to automate the process of exporting an Excel worksheet to a PDF file, where the file name is dynamically generated based on a selected value from a data-validation list in cell B2.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Automating document generation by cycling through a drop-down list and saving each unique iteration as a standalone PDF in a specific folder.
- Observed behavior
- The user requires specific VBA code to capture the value of cell B2, concatenate it with a predefined output folder path, and execute the PDF export function.
Before running the macro, verify that your target destination folder exists on your computer and ensure that none of the values in your data-validation list contain forbidden file name characters such as slashes (/ \), colons (:), or asterisks (*).
Add the ExportAsFixedFormat Method to Your Macro
Modify your existing VBA loop to dynamically build the file path using the data validation cell's value and use the ExportAsFixedFormat command to save the PDF.
To accomplish this, you need to declare a string variable in your VBA code that combines your target directory path with the value present in your data validation cell (e.g., B2). Once the path string is built, you can call Excel's native PDF export method.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor, and navigate to the module containing your data validation loop.
Inside your loop, declare a string variable for the file name by typing: Dim filename As String.
Assign the folder path and the cell value to the variable. For example, add the line: filename = "D:\Test\" & Range("B2").Value & ".pdf" (update 'D:\Test\' to your actual folder path).
Below the file name definition, insert the following code to save the PDF: ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=filename, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True.

Automate Workflows and Export PDFs Seamlessly in WPS Spreadsheet
WPS Spreadsheet offers powerful support for VBA macros alongside native, high-quality PDF exporting capabilities. You can execute advanced automation scripts just as you would in Microsoft Excel.
- 1. Open Your Workbook: Launch WPS Spreadsheet and open the workbook containing your data validation lists.
- 2. Access the Developer Tools: Navigate to the Developer tab on the top ribbon. If it is hidden, you can enable it in the WPS Options menu.
- 3. Launch the VBA Editor: Click the VBA Editor button (or press Alt + F11) to view and edit your macro scripts.
- 4. Run the PDF Export Macro: Paste your dynamic PDF export code and click Run. WPS will seamlessly generate and save the PDFs to your specified folder.

Frequently Asked Questions
Why am I getting a 'Run-time error 1004' when the macro tries to save the PDF?
This error most commonly occurs for two reasons: the target folder path specified in your VBA code (e.g., 'D:\Test\') does not exist, or the value in cell B2 contains invalid characters for Windows file names, such as < > ? [ ] : | or *.
Can I export only a specific range instead of the entire ActiveSheet?
Yes. Instead of using 'ActiveSheet.ExportAsFixedFormat', you can specify a range. Replace 'ActiveSheet' with 'Range("A1:G20")' or whatever specific cell block you wish to convert to PDF.
How can I automatically create the output folder if it doesn't exist?
You can use the 'Dir' function in VBA to check if the folder exists, and the 'MkDir' command to create it. Add this logic before the ExportAsFixedFormat command to ensure the destination is always available.
Will this macro work if I place the data validation list on a different sheet?
Yes, but you must explicitly reference that sheet in your code. Instead of simply using 'Range("B2").Value', use 'Sheets("YourSheetName").Range("B2").Value' to ensure the macro pulls the file name from the correct worksheet.




