VBA & Macro Problems

How to Save Excel as PDF with Cell-Based Filename via VBA

Adam DavisAdam Davis Oct 29, 2026 869 views

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.

How to Save Excel as PDF with Cell-Based Filename via VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.

2
Locate Your Macro Module

In the Project Explorer pane on the left, find and double-click the module containing your current PDF export macro.

3
Set the Dynamic Path Variable

Define a string variable for the path using `Environ("USERPROFILE") & "\Documents\"` to point to the current user's Documents folder.

4
Fetch the Cell Value

Set your filename variable to read the specific cell value: `FileName = ThisWorkbook.Sheets("Lift Plan").Range("P17").Value & ".pdf"`.

5
Execute the PDF Export

Use the ExportAsFixedFormat method on the worksheet, passing the combined folder path and filename string as the Filename parameter.

Update the VBA Macro to Read the Cell Value and User Profile
Avoid Invalid Characters: Ensure that the value in cell P17 does not contain characters that are invalid for Windows file names, such as slashes (/ \), colons (:), or asterisks (*).

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. 1. Download and Install: Get WPS Office for free from the official website and install it on your computer.
  2. 2. Open your Macro Workbook: Launch WPS Spreadsheet and open your .xlsm file containing the Lift Plan data and macro.
  3. 3. Enable Macros: Click 'Enable Macros' when prompted by the security warning banner at the top of the worksheet.
  4. 4. Run the PDF Macro: Navigate to the 'Developer' tab, click 'Macros', select your custom PDF export macro, and click 'Run'.
Fully compatible with Microsoft Excel formats (.xlsx, .xlsm, .xls)Robust VBA/Macro support for advanced spreadsheet automation tasksBuilt-in PDF converter to instantly save dynamic sheets as PDFLightweight application with a familiar, easy-to-use interface
microsoft office alternative - wps office

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.