So I have a master document and several hundred files with different date suffixes in the same folder. On the master document I want to list the date code and some various information in the file itself. The folder will be updated regularly so I want to be able to run the report periodically and add any new dates to the list.
So how do I do the following:- Search each file name in a folder
- If the file is on my list, skip it. Else open the file.
- Preform a function to the open file.
- Close the file and proceed to continue checking the folder.
I cobbled together the below from various sources but it seems to have the problem of opening every file after a new file is found (eg if I have 100 files in the folder and only 97 is new, it will open file 97 to 100).
Sub ListFilesInFolder(ByVal SourceFolderName As String, ByVal IncludeSubfolders As Boolean)
Dim FSO, SourceFolder, SubFilder, FileItem As Object
Dim r As Long
Set FSO = CreateObject("Scripting.FileSystemObject")
Set SourceFolder = FSO.GetFolder(SourceFolderName)
OrgWb = ThisWorkbook.Name
OrgWs = ActiveSheet.Name
Directory = "K:\Production\Robots\"
r = Range("A65536").End(xlUp).Row + 1
For Each FileItem In SourceFolder.Files
FileDate = Left(Right(FileItem.Name, 12), 8)
Set Result = Cells.Find(What:=FileDate, After:=Cells(1, 1), _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If (Not Result Is Nothing) Then
Else
FilePath = Directory & FileItem.Name
Workbooks(OrgWb).Worksheets(OrgWs).Cells(r, 1) = FileDate
Workbooks.Open (FilePath)
'Do Suff with Open Book
Workbooks.Close (FilePath)
r = r + 1
End If
X = SourceFolder.Path
Next FileItem
If IncludeSubfolders Then
For Each SubFolder In SourceFolder.SubFolders
ListFilesInFolder SubFolder.Path, True
Next SubFolder
End If
Set FileItem = Nothing
Set SourceFolder = Nothing
Set FSO = Nothing
End Sub
Bookmarks