VBA FileDateTime Function

Written by

Editorial Team

Reviewed by

Steve Rynearson

Last updated on July 19, 2021

FileDateTime Description

Returns the date and time of when a file was created or last modified.

Simple FileDateTime Example

Assume a file “D:\test.txt” was last modified on 10/21/2019 9:41:30 AM .

MsgBox FileDateTime("D:\test.txt")

This will return 10/21/2019 9:41:30 AM.

FileDateTime Syntax

In the VBA Editor, you can type  “FileDateTime(” to see the syntax for the FileDateTime Function:

The FileDateTime function contains an argument:

PathName: A string expression representing a file/folder/drive.

Examples of Excel VBA FileDateTime Function

To list the last modified time of the folder&files on C drive, you can use the following code.

Sub Dir_Example()
    Dim fileName    As String
    Dim fullName    As String
    Dim rng         As Range
    Dim i           As Integer

    Set rng = Range("A1")
    fileName = Dir("C:\", vbDirectory)
    i = 1
        fullName = "C:\" & fileName
        rng.Offset(i, 0) = fileName
        rng.Offset(i, 1) = FileDateTime(fullName)
        rng.Offset(i, 2) = FileLen(fullName)
        rng.Offset(i, 3) = GetAttr(fullName)
        fileName = Dir
        If fileName = "" Then Exit Do
        i = i + 1
End Sub

The result will be similar with the following.


VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! vba save as

Learn More!

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples