Hi Tim ... the code below goes in a standard module and will search through the directory in which the file is saved (so save it somewhere before running it) and all sub-directories looking for files that start with "Ratios" and progressively identifying the latest such file. When complete it will open the file with the latest create date.
Option Explicit
Public datNewest As Date
Public strLatestFile As String
Sub OpenLatestRatioFile()
Dim strRootDir As String
datNewest = CDate("01 Jan 0001")
strRootDir = ThisWorkbook.Path & "\"
strLatestFile = ""
Call GetSubDirectories(strRootDir)
If strLatestFile <> "" Then
Workbooks.Open Filename:=strLatestFile, UpdateLinks:=False, ignorereadonlyrecommended:=True
Else
MsgBox ("No file beginning with Ratios found in directory or subfolders.")
End If
End Sub
Sub GetSubDirectories(strFolder As String)
Dim objFileSystem As Object: Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Dim objFolder As Object: Set objFolder = objFileSystem.GetFolder(strFolder)
Dim objSubFolder As Object
Call GetFiles(objFolder.Path)
For Each objSubFolder In objFolder.subfolders
GetSubDirectories (objFolder.Path & "\" & objSubFolder.Name) 'recursive call
Next objSubFolder
End Sub
Sub GetFiles(strPath As String)
Dim objFile As Object
Dim objFileSystem As Object: Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Dim objFolder As Object: Set objFolder = objFileSystem.GetFolder(strPath)
On Error Resume Next
For Each objFile In objFolder.Files
If objFile.DateCreated > datNewest And Left(objFile.Name, 6) = "Ratios" Then
datNewest = objFile.DateCreated
strLatestFile = strPath & "\" & objFile.Name
End If
Next objFile
On Error GoTo 0
End Sub
If you want to specify a different starting directory then change this line in the first routine:
strRootDir = ThisWorkbook.Path & "\"
To something like:
strRootDir = "C:\Users\Tim\"
Remember the path must end with an "\".
Hope that helps. MM.
Bookmarks