VBA & Macro Problems

Compare a PDF File Date With Today Using Excel VBA

Olivia MillerOlivia Miller Nov 14, 2026 868 views

Question details

Find a PDF training file with Excel VBA and compare its creation or modification date with today's date.

Compare a PDF File Date With Today Using Excel VBA
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A macro needs to search a folder for a training PDF, display a date-related message, and stop if a matching file was created today.
Observed behavior
The macro should confirm when a matching PDF has today's date or report the dates of matching files when none match.
Before you start

Have the folder containing the training PDF available and check its filename so you can adjust the matching pattern. The FileSystemObject example below is intended for desktop Excel on Windows; enable macros only for workbooks you trust.

Solution 1Recommended

Compare PDF creation dates with FileSystemObject

Use this macro to select a folder, search for training PDFs, and stop as soon as a matching file was created today.

The macro uses late binding, so you do not need to add a Microsoft Scripting Runtime reference. Int removes the time portion of the creation timestamp before comparing it with the computer's current date.

1
Open the VBA editor

In desktop Excel, press Alt+F11, then select Insert > Module.

2
Add the macro

Paste this code into the module: Sub CheckTrainingPDFDate() Dim fso As Object, folder As Object, file As Object Dim picker As FileDialog Dim folderPath As String, dates As String Dim found As Boolean Set picker = Application.FileDialog(msoFileDialogFolderPicker) picker.Title = "Select the folder containing the training PDF" If picker.Show <> -1 Then Exit Sub folderPath = picker.SelectedItems(1) Set fso = CreateObject("Scripting.FileSystemObject") Set folder = fso.GetFolder(folderPath) For Each file In folder.Files If LCase(file.Name) Like "training*.pdf" Then found = True If Int(file.DateCreated) = Date Then MsgBox file.Name & " was created today." Exit Sub End If dates = dates & file.Name & ": " & _ Format(file.DateCreated, "yyyy-mm-dd") & vbCrLf End If Next file If found Then MsgBox "No matching PDF was created today." & vbCrLf & dates Else MsgBox "No matching training PDF was found." End If End Sub

3
Adjust the filename pattern

In the line containing Like "training*.pdf", replace the pattern if your filename differs. The asterisk matches any characters between "training" and ".pdf".

4
Run the macro

Return to Excel, press Alt+F8, select CheckTrainingPDFDate, and click Run. Choose the PDF's folder in the folder picker and read the message.

Compare PDF creation dates with FileSystemObject
Compare the modification date instead: Replace both occurrences of file.DateCreated with file.DateLastModified. Creation dates can reflect when a file was copied to its current location rather than when its content was originally authored.
Free Microsoft Office alternative

Manage Training Spreadsheets With WPS Office

WPS Office is a free, lightweight alternative for working with spreadsheets and PDFs. Its familiar interface and Microsoft Office format compatibility make it easy to move existing files; Excel VBA and FileSystemObject compatibility can vary and should be tested separately.

Open and review spreadsheets used to track training filesCompatible with Microsoft Excel formats including XLSX and XLSView PDF files alongside your office documentsUse a familiar interface for a straightforward transition from Microsoft Office
QA img-9

Frequently Asked Questions

How do I compare the PDF's modified date instead of its creation date?

In the VBA module, replace both uses of file.DateCreated with file.DateLastModified, then run the macro again from Alt+F8.

Why does the macro use Int before comparing the date?

File timestamps include a time. Int(file.DateCreated) removes the time portion so it can be compared with VBA's Date value for today.

What if my PDF has a different name?

Change the Like pattern in the VBA module. For example, use "safety training*.pdf" to match PDF names beginning with "safety training".

Does the macro search subfolders?

No. The For Each file In folder.Files loop checks only files in the folder selected in the picker. Searching subfolders requires additional code.

What happens if several training PDFs match the pattern?

The macro stops at the first matching PDF created today. If none were created today, it displays the creation dates of all matching PDFs in the selected folder.