PDA

View Full Version : Find All files in a Folder that were created and/or Modified after a set date (x), Op



ECoryn
07-01-2014, 08:16 AM
I need to find all of the files in a Folder that were created and/or modified after a set date (x)

After these files are found, I will need to open each one of them, copy the data contained within them, and paste it into a master spreadsheet.

After doing this, I will reset the date (X) to the date the most recent file that was modified (prior to me opening it).
I believe I know how to do the code to copy and paste the data from one spreadsheet to another; however, I need help on the following code: Finding the newest files (From date X), opening the files, closing the files.

I've seen some codes using Scripting.FileSystemObject or Scripting.Folder or Scripting.File. But I am unfamiliar with what these codes are actually doing and have been unable to modify them for my purposes.

Any help would be much appreciated; I have searched many forums/sites but have had no luck modifying the codes I found in an effective way for my purposes.

(*The codes I have seen, but do not understand:'cpearson.com/excel/getfilename.aspx')

yoslick11
07-01-2014, 12:53 PM
Sub LoopThroughFiles()
Dim fso As Object
Dim file As Object
Dim myFile As Variant
Dim strFile As String
Dim strDate As String
Dim folder As String
Dim dt As String
Dim highDate As String
dt = "9/13/2014 12:00:00 PM"
highDate = dt 'initialize
folder = "FOLDER NAME HERE ENDING WITH \"
Set fso = CreateObject("Scripting.FileSystemObject")
myFile = Dir(folder & "*xlsx*")

Do While Len(myFile) > 0
Set file = fso.GetFile(folder & myFile)
strDate = file.DateLastModified
If CDate(strDate) > CDate(dt) Then
If CDate(strDate) > CDate(highDate) Then highDate = CDate(strDate)

'Copy and Paste data here

End If
myFile = Dir
Set file = Nothing
Loop
Set fso = Nothing

'If highDate = dt then MsgBox "No files were found with a later date"

End Sub

highDate will be your new latest rev date