How to get the date modified for a user defined filename in cell.

omairhe

Well-known Member
Joined
Mar 26, 2009
Messages
2,040
Office Version
  1. 2019
Platform
  1. Windows
I want to get the date last modified for a user-defined excel filename in cell D2.
D2 =
D:\Users\Michael Phelps\03-2021.xlsb

Answer: 3/5/2021 1:06:34 PM

Is this possible with formula? Thanks.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Maybe using a "user defined function":
1) copy this code into a standard vba module of your vba project
Code:
Function GetFileInfo(ByVal myFile As String, Optional ByVal myPar As Long = 1) As Variant
'see https://www.mrexcel.com/board/threads/how-to-get-the-date-modified-for-a-user-defined-filename-in-cell.1163898/
Dim myFSO As Object
Set myFSO = CreateObject("Scripting.FileSystemObject")
If myPar = 1 Then
    GetFileInfo = myFSO.GetFile(myFile).DateCreated
ElseIf myPar = 2 Then
    GetFileInfo = myFSO.GetFile(myFile).DateLastAccessed
ElseIf myPar = 3 Then
    GetFileInfo = myFSO.GetFile(myFile).DateLastModified
Else
    GetFileInfo = CVErr(2042)
End If
Set fso = Nothing
End Function

2) Then return to your worksheet, you will be allowed to use a formula like this:
Code:
=GetFileInfo(FileFullPath&Name;Index)
-FileFullPath&Name is the path + name of the file; could be a string, or a reference to a cell that contains the string, or a formula that create a correct string
-Index can be 1 (the default value)=DateCreated; or 2=DateLastAccessed; or 3=DateLastModified

Examples for valid formulas:
Code:
=GetFileInfo(K12,3)                               'DateLastModified
=GetFileInfo(K12)                                       'DateCreated
=GetFileInfo("D:\Dropbox\SHARED\PCF_Workload.xlsm",3)   'string in formula
=GetFileInfo(A2&"\"&B2,3)                               'A2 is the Path, B2 is the Finename

Bye
 
Upvote 0
Solution
Maybe using a "user defined function":
1) copy this code into a standard vba module of your vba project
Code:
Function GetFileInfo(ByVal myFile As String, Optional ByVal myPar As Long = 1) As Variant
'see https://www.mrexcel.com/board/threads/how-to-get-the-date-modified-for-a-user-defined-filename-in-cell.1163898/
Dim myFSO As Object
Set myFSO = CreateObject("Scripting.FileSystemObject")
If myPar = 1 Then
    GetFileInfo = myFSO.GetFile(myFile).DateCreated
ElseIf myPar = 2 Then
    GetFileInfo = myFSO.GetFile(myFile).DateLastAccessed
ElseIf myPar = 3 Then
    GetFileInfo = myFSO.GetFile(myFile).DateLastModified
Else
    GetFileInfo = CVErr(2042)
End If
Set fso = Nothing
End Function

2) Then return to your worksheet, you will be allowed to use a formula like this:
Code:
=GetFileInfo(FileFullPath&Name;Index)
-FileFullPath&Name is the path + name of the file; could be a string, or a reference to a cell that contains the string, or a formula that create a correct string
-Index can be 1 (the default value)=DateCreated; or 2=DateLastAccessed; or 3=DateLastModified

Examples for valid formulas:
Code:
=GetFileInfo(K12,3)                               'DateLastModified
=GetFileInfo(K12)                                       'DateCreated
=GetFileInfo("D:\Dropbox\SHARED\PCF_Workload.xlsm",3)   'string in formula
=GetFileInfo(A2&"\"&B2,3)                               'A2 is the Path, B2 is the Finename

Bye

Thank you Anthony.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top