One example could be that you receive daily files which are the basis for a regular report you compile in Excel.
18-3-2012Report.csv 19-3-2012Report.csv 20-3-2012Report.csvListing Files With The File System Object
The Microsoft Scripting Runtime library must be selected under references in the tools tab before any code you write can access the file system.
Our short piece of VBA code will select all the files in the appropriate folder and find the file with the most recent file creation date.
First, we'll create the file system object and select the folder, which in this example is called "files" and is under the folder of the active workbook.
Dim fso As Scripting.FileSystemObject
Dim SourceFolder As Scripting.FolderSet fso = New Scripting.FileSystemObjectNext, we can loop through the folder and determine the most recent file. We do this by creating a date object, which by default defines the year as 1899 - depending on the version of Excel you have.
Set SourceFolder = fso.GetFolder(ActiveWorkbook.path & "\files")
To begin, we set the newest date as "30/12/1899" so every file will have been created after that date. As we loop through the code we redefine the newest date and file depending on the file creation date.
Dim d As Date newestFile = "" newestDate = d