logo
search
VBA & Macro Problems

How to Add the Current Date or Julian Date to an Excel PDF Filename via VBA

Bushra ParveenBushra Parveen Oct 16, 2026 870 views

Question details

The user wants to automate saving an Excel worksheet as a PDF using VBA, ensuring the filename dynamically includes the current date, a Julian date, or a UTC timestamp.

Add the Current Date or Julian Date to an Excel PDF Filename using VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
Automating document generation and archiving where unique, timestamped filenames are required to prevent overwriting and maintain organized file records.
Observed behavior
The user requires the correct VBA syntax and date functions to dynamically construct the filename string and pass it to the ExportAsFixedFormat method.
Before you start

Ensure that your Excel workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have enabled Developer tools and macros in your settings.

Solution 1Recommended

Use VBA Format Function for Standard Date

Construct a dynamic filename string using the Format function combined with the current date before saving the worksheet as a PDF.

To avoid naming conflicts and automatically track when a report was generated, you can use the VBA 'Format' function. This allows you to append the current system date to your filename in a structured, readable format before executing the PDF export command.

1
Open the VBA Editor

Press 'Alt + F11' on your keyboard to open the Visual Basic for Applications (VBA) Editor. Insert a new module by clicking 'Insert' > 'Module' in the top menu.

2
Define Your Variables

Inside your new macro subroutine, declare variables to hold your file path, formatted date, and the final combined filename. Use 'Dim strPath As String', 'Dim strDate As String', and 'Dim strFile As String'.

3
Format the Current Date

Assign the current date to your variable using the Format function. For example, type 'strDate = Format(Date, "yyyy-mm-dd")' to generate a URL-safe and standard date string.

4
Combine and Export to PDF

Build the full file path by concatenating your variables: 'strFile = strPath & "Report_" & strDate & ".pdf"'. Finally, save the PDF by executing 'ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFile'.

Use VBA Format Function for Standard Date
Avoid Invalid Characters: Never use slashes (/) or colons (:) in your Date formatting string (like "mm/dd/yyyy"), as Windows does not allow these characters in filenames.
Seamless VBA & Macro Support

Automate Your Workflows with WPS Spreadsheet

WPS Office provides robust built-in support for VBA and macros, allowing you to run the exact same PDF export scripts effortlessly. It serves as a highly compatible, feature-rich suite for handling complex data automation tasks directly.

  1. 1. Download and Install WPS Office: Visit the official WPS website, download the free suite, and install it on your PC or Mac.
  2. 2. Open Your Macro-Enabled Workbook: Launch WPS Spreadsheet and open your existing .xlsm file containing the PDF export VBA scripts.
  3. 3. Enable Macros: If prompted by the security warning at the top of the worksheet, click 'Enable Macros' to allow your automation scripts to run.
  4. 4. Run the PDF Export Macro: Navigate to the 'Developer' tab, click on 'Macros', select your PDF export script from the list, and click 'Run'. Your dynamically named PDF will be created instantly.
Fully compatible with Microsoft Excel (.xlsm, .xlsx) files and macro scripts.Built-in PDF exporter to seamlessly save dynamic reports.Lightweight installation and fast execution of complex VBA tasks.Familiar ribbon interface, requiring zero learning curve for Excel users.
microsoft office alternative - wps office

Frequently Asked Questions

Can I include the exact time (hours and minutes) in the PDF filename?

Yes, you can use the VBA 'Now' function instead of 'Date' and format it to include the current time. For example, 'Format(Now, "yyyy-mm-dd_hhmmss")' creates a safe timestamp like 2023-10-25_143000. Avoid using colons (:) as they are invalid in Windows filenames.

Why am I getting an error when using ExportAsFixedFormat in VBA?

This typically happens if the specified destination folder path does not exist, you lack write permissions to that directory, or your generated filename contains invalid characters (like /, \, :, *, ?, ", <, >, or |). Double-check your date formatting string and folder path.

Does WPS Spreadsheet support exporting to PDF via VBA?

Yes, WPS Spreadsheet has excellent macro and VBA compatibility. You can use the exact same 'ExportAsFixedFormat' VBA method to save your worksheets as PDFs directly within the WPS Spreadsheet environment.

What is a Julian Date and why use it in filenames?

In modern business computing, a Julian date often refers to the continuous count of days within the current year (e.g., '23045' representing the 45th day of 2023). It is frequently used in manufacturing and data logging to create precise, sortable, and compact daily version names.