Hi,
Within each child folder, I want to loop through opening all of the .xlsm files with the most recent creation date (not last modified date). Once open, I'll write additional code to modify and save as a new file.
I have written code that allows me to loop through opening ALL files across child folders, but I can't figure out how/where to apply file system object to only open on the most recent date created parameter. Thank you in advance for any insights!
Within each child folder, I want to loop through opening all of the .xlsm files with the most recent creation date (not last modified date). Once open, I'll write additional code to modify and save as a new file.
I have written code that allows me to loop through opening ALL files across child folders, but I can't figure out how/where to apply file system object to only open on the most recent date created parameter. Thank you in advance for any insights!
Code:
Sub OneType()
Const MyPath = "C:\Users\Otawata\Desktop\FY25\" ' Set the path.
Const FileType = "*.xlsm" 'Set file type and/or name/naming convention to match
ProcessFiles MyPath, FileType
End Sub
Sub OneName()
Const MyPath = "C:\Users\Otawata\Desktop\FY25\" ' Set the path.
Const FileName = "*.xlsm" 'Set file type and/or name/naming convention to match
ProcessFiles MyPath, FileName
End Sub
Sub ProcessFiles(strFolder As String, strFilePattern As String)
'Define and set variables
Dim strFileName As String
Dim strFolders() As String
Dim iFolderCount As Integer
Dim i As Integer
'Setting to allow this to run more efficiently
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'Collect child folders
strFileName = Dir$(strFolder & "\", vbDirectory)
Do Until strFileName = ""
If (GetAttr(strFolder & "\" & strFileName) And vbDirectory) = vbDirectory Then
If Left$(strFileName, 1) <> "." Then
ReDim Preserve strFolders(iFolderCount)
strFolders(iFolderCount) = strFolder & "\" & strFileName
iFolderCount = iFolderCount + 1
End If
End If
strFileName = Dir$()
Loop
'Process files in current folder
strFileName = Dir$(strFolder & "\" & strFilePattern)
Do Until strFileName = ""
'********Do things with files here*****************
'**************************************************
strFileName = Dir$()
Loop
'Look through child folders and repeat the above actions
For i = 0 To iFolderCount - 1
ProcessFiles strFolders(i), strFilePattern
Next i
End Sub