Return to VBA Code Examples

VBA FileDateTime Function

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
    
    Do
        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
    Loop
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!