VBA Code for Date Created and Date Modified

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
439
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance and feedback will be given for any suggestions.

I've done an exhaustive search and anything I find and try to incorporate is giving issues. How do I add the date created and date modified to this file. It currently get the file name and places it in column A, and I would like the date created and date last modified to be in columns B and C respectively.

These lines I was trying to code to get that:

Code:
'Sheets("List").Cells(i, 2).Value = Date created should be coded in this line
'Sheets("List").Cells(i, 2).Value = Date modified should be coded in this line


Code:
Sub GetFileNames()


'Dimensioning
    Dim Folder As String
    Dim ObjFSO As Object
    Dim ObjFolder As Object
    Dim ObjFile As Object
    Dim i As Integer


    
    If Right(Sheets("List").Cells(3, 2).Value, 1) <> "\" Then
    
        Sheets("List").Cells(3, 2).Value = Sheets("List").Cells(3, 2).Value & "\"
    
        End If
        
    
    DirFolderRename = Sheets("List").Cells(3, 2).Value


    i = 5


    Set ObjFSO = CreateObject("Scripting.FileSystemObject")
    Set ObjFolder = ObjFSO.GetFolder(DirFolderRename)
    For Each ObjFile In ObjFolder.Files
        
        Sheets("List").Cells(i, 1).Value = ObjFile.Name
        'Sheets("List").Cells(i, 2).Value = Date created should be coded in this line
        'Sheets("List").Cells(i, 2).Value = Date modified should be coded in this line

    i = i + 1

    Next ObjFile

End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
See here: https://www.mrexcel.com/forum/excel...l-file-properties-date-created-using-vba.html for details on how to pull those particular properties.

Also note another issue with your commented out code:
Code:
'Sheets("List").Cells(i, 2).Value = Date created should be coded in this line
'Sheets("List").Cells(i, 2).Value = Date modified should be coded in this line
They would be overwriting each (both writing to column B). To put in columnss B, and C respectively, it should be:
Code:
'Sheets("List").Cells(i, 2).Value = Date created should be coded in this line
'Sheets("List").Cells(i, [COLOR=#ff0000][B]3[/B][/COLOR]).Value = Date modified should be coded in this line
 
Last edited:
Upvote 0
Thanks Joe4 for your response and correction to the code I did have written as that was an oversite by me.

I did see the exact post for the link you left, but when I modified my code it did not work.

I tried the following and I got an error:

Can you give me an indication what I have coded incorrectly?

Code:
Sheets("List").Cells(i, 2).Value = ObjFileName.Datelastmodified
Sheets("List").Cells(i, 3).Value = [COLOR=#333333]ObjFileName.DateCreated[/COLOR]
 
Upvote 0
This is the code which I got to work.

Code:
        Sheets("List").Cells(i, 2).Value = ObjFile.DateCreated
        Sheets("List").Cells(i, 3).Value = ObjFile.DateLastModified

The code in it's entirety is as follows.

Code:
Sub GetFileNames()


'Dimensioning
    Dim Folder As String
    Dim ObjFSO As Object
    Dim ObjFolder As Object
    Dim ObjFile As Object
    Dim i As Integer
    
'Obtaining the directory of where the files of interest reside
    If Right(Sheets("List").Cells(3, 2).Value, 1) <> "\" Then
    
        Sheets("List").Cells(3, 2).Value = Sheets("List").Cells(3, 2).Value & "\"
    
   End If
        
    
    DirFolderRename = Sheets("List").Cells(3, 2).Value


    i = 5


    Set ObjFSO = CreateObject("Scripting.FileSystemObject")
    Set ObjFolder = ObjFSO.GetFolder(DirFolderRename)
    
    For Each ObjFile In ObjFolder.Files
        
        Sheets("List").Cells(i, 1).Value = ObjFile.Name
        Sheets("List").Cells(i, 2).Value = ObjFile.DateCreated
        Sheets("List").Cells(i, 3).Value = ObjFile.DateLastModified
      
        
    i = i + 1
    
    
    
    Next ObjFile




End Sub
 
Upvote 0
Right, it needs to match the name of the object variable you created for it.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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