• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Open on Most Recent Creation Date (Loop through all Child folders)

Otawata

New Member
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!

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
 
I think it could also work to search child folders for file names for newer vs. older date strings in the file names...

For example:
- Is there a 9-1 file? Yes --> Open 9-1 file and execute changes No --> go to next
- Is there an 8-1 file? Yes --> Open 8-1 file and execute changes No --> go to next
- Is there a 7-1 file? Yes--> Open 7-1 file and execute changes ... etc.
 
Back
Top