It is sometimes useful to know the date and time a file was last modified. this is particularly useful if you have a folder full of similar files and need to use the latest copy.
Today the code I’m going to share below enables you to pass a file address into a function which will return the date and time it was last saved. The example below passes a file address to the function, which returns the modified date, this is then displayed in a message box.
The function requires a reference to Microsoft Scripting Runtime within the VBE.
Dim ModDate As Date
ModDate = FileLastModDate("C:\YourFileAddressGoesHere")
Function FileLastModDate(strFileAddress As String) As Date
' In the VBE, set a reference to Microsoft Scripting runtime
Dim fso As Scripting.FileSystemObject
Dim fsof As Scripting.File
Dim strPath As String
Set fso = New FileSystemObject
strPath = strFileAddress
Set fsof = fso.GetFile(strPath)
FileLastModDate = .DateLastModified
Set fso = Nothing
Set fsof = Nothing
As always, if you have any questions do let us know via the comments section.