How to Save Excel as PDF with Cell-Based Filename via VBA
Question details
The user needs a VBA macro to export an Excel worksheet as a PDF, automatically generating the filename from the value in cell P17 of the Lift Plan worksheet, and saving it to the current user's Documents folder.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Automating PDF document generation where the file name must dynamically update based on cell content for different users.
- Observed behavior
- The current macro saves the PDF using a fixed, static filename and path instead of dynamically reading the user's cell data and profile directory.
Ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have enabled macros in your spreadsheet application's Trust Center settings.
Update the VBA Macro to Read the Cell Value and User Profile
Modify your existing VBA code to dynamically fetch the filename from cell P17 and use the Windows environment variable to construct the path to the current user's Documents folder.
By using the `Environ` function, the macro can dynamically identify the active user's document path, preventing errors when different users run the script. We then combine this path with the cell value to form the complete file path.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
In the Project Explorer pane on the left, find and double-click the module containing your current PDF export macro.
Define a string variable for the path using `Environ("USERPROFILE") & "\Documents\"` to point to the current user's Documents folder.
Set your filename variable to read the specific cell value: `FileName = ThisWorkbook.Sheets("Lift Plan").Range("P17").Value & ".pdf"`.
Use the ExportAsFixedFormat method on the worksheet, passing the combined folder path and filename string as the Filename parameter.

Consult Microsoft Tech Community for Advanced Customizations
If you are encountering errors with complex Office programming features, the Microsoft Q&A forum is the recommended place for advanced VBA troubleshooting.
Run VBA and Automate Workflows with WPS Office
WPS Spreadsheet offers powerful support for VBA macros, allowing you to seamlessly run your custom scripts, export dynamic PDFs, and automate your daily tasks just like in Microsoft Excel.
- 1. Download and Install: Get WPS Office for free from the official website and install it on your computer.
- 2. Open your Macro Workbook: Launch WPS Spreadsheet and open your .xlsm file containing the Lift Plan data and macro.
- 3. Enable Macros: Click 'Enable Macros' when prompted by the security warning banner at the top of the worksheet.
- 4. Run the PDF Macro: Navigate to the 'Developer' tab, click 'Macros', select your custom PDF export macro, and click 'Run'.

Frequently Asked Questions
Why does my VBA macro fail when saving the PDF?
The macro typically fails if the referenced cell for the filename (e.g., P17) contains invalid characters like <, >, ?, *, :, or |, or if the specified folder path does not exist on that specific user's computer.
How do I get the current user's Documents folder path in VBA?
You can retrieve the current user's profile path dynamically by using the Environ("USERPROFILE") function in VBA and appending "\Documents\" to it.
Can I run Excel VBA macros in WPS Office?
Yes, WPS Office supports VBA macros. Ensure you are using a version of WPS Office (such as Pro or Business) that includes the VBA module, which provides high compatibility with standard Excel VBA scripts.
How do I ensure the PDF saves as a single page in VBA?
Before calling ExportAsFixedFormat, set the worksheet's PageSetup properties. You can configure FitToPagesWide = 1 and FitToPagesTall = 1 to scale the content to fit onto one page in the exported PDF.




