VBA FileDateTime Function

VBA FileDateTime Function

Overview of VBA FileDateTime Function:


VBA FileDateTime function is categorized as a File and Directory function. This built-in VBA FileDateTime function returns the date and time when a file or directory was created or last modified in Excel VBA. This function works for files, directories and drives. It returns a date value.
This function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA FileDateTime Function in any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the FileDateTime function, where we can use this FileDateTime Function and real-time examples in Excel VBA.

Syntax of VBA FileDateTime Function

The syntax of the FileDateTime Function in VBA is

FileDateTime(PathName)

This FileDateTime function returns a date value. We can format date using format function.

Read More about VBA Format Function

Parameters or Arguments:

The FileDateTime function has one argument in Excel VBA.
where
PathName: It is a mandatory string parameter. The PathName argument represents the path of the file name. It helps to know the created or last modified date of file. If we ignore this parameter, returns an error.

Where we can apply or use VBA FileDateTime Function?

We can use this FileDateTime Function in VBA MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.

Example 1: Find Last Modified Date of Specified File

Here is a simple example of the VBA FileDateTime function. This below example finds last modified dtae of specified file.

'Find last modified date of specified file
Sub VBA_FileDateTime_Function_Ex1()
    
    Dim sFilePath As String
    Dim dOutput As Date
    
    sFilePath = "C:\Someswari\VBAF1\VBA Functions\VBA Text Functions\VBA Function Example File.xlsm"
    
    dOutput = FileDateTime(sFilePath)
    
    MsgBox "Last Modified Date of File : " & vbCrLf & dOutput, vbInformation, "VBA FileDateTime Function"
   
End Sub

Output: Here is the screen shot of the first example output.
VBA FileDateTime Function

Example 2: Find Specified File Creation Date(Returns an Error)

Here is a simple example of the VBA FileDateTime function. This below example returns an error. Because, the specified file is not available.

'Find specified file create date
Sub VBA_FileDateTime_Function_Ex2()
    
    Dim sFilePath As String
    Dim dOutput As Date
    
    'Note: This file is not available
    sFilePath = "C:\VBA Functions\VBA Text Functions\VBA Function Example File.xlsm"
    
    dOutput = FileDateTime(sFilePath)
    
    MsgBox "Last modified date of file : " & dOutput, vbInformation, "VBA FileDateTime Function"
    
End Sub

Output: Here is the screen shot of the second example output.
VBA FileDateTime Function

Instructions to use Macro Codes

Here are the step by step instructions to use the FileDateTime Function in Excel VBA.

  • Open an Excel workbook.
  • Press Alt+F11 to open VBA Editor window.
  • Go to Insert menu and click on module from the available options.
  • Copy above specified macros to the VBA editor.
  • Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.

List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.

VBA File & Directory Functions List of VBA Functions

Leave a Comment

Your email address will not be published. Required fields are marked *