Basically, I have a list of folder path in the excel worksheet as shown in figure below: List of Folder Path
I want to loop through all the subfolders(up to 3, 4 layers) to find the folder named "Report" , every folder path which listed in the figure contain the "Report" folder.
Sub SearchReport()
Dim FileSystem As Object
counter = 2
Do While Range("A" & counter).Value <> "" 'do when Range A is not empty (folder path)
If Range("C" & counter).Value = "Yes" Then 'check Range C, do when Range C = Yes
HostFolder = Range("A" & counter).Value & "\"
Set FileSystem = CreateObject("Scripting.FileSystemObject")
Report FileSystem.getFolder(HostFolder) 'HostFolder is the folder path from Range A
counter = counter + 1
Else
counter = counter + 1
End If
Loop
End Sub
Sub Report(Folder)
Dim SubFolder
Dim subfld As Folder
Dim subfldr As Folder
Dim File As File
Dim MyPath As String
Dim Wksht As Worksheet
Dim wbk As Workbook
For Each SubFolder In Folder.SubFolders 'loop through subfolders in the first folder path
Do While SubFolder.Name = "Report" 'look for a folder named "Report"
MyPath = SubFolder.Path & "\"
Filename = Dir(MyPath & "*al.dat") 'look for files which is ended with "al.dat" in the "Report" folder
Do While Len(Filename) > 0 'open the files
Set Wksht = Worksheets.Add
Wksht.Name = Left(Filename, Len(Filename) - 10)
Set wbk = Workbooks.Open(MyPath & Filename)
Set Wrksht = wbk.Worksheets(1)
Wrksht.Cells.Copy Wksht.Cells
wbk.Close True
Filename = Dir
Loop
Loop
Report SubFolder
Next
End Sub
I am able to loop through the subfolder to search for the "Report" folder after I modified the code obtained from the net, and also import all the files in the folder. The problem is, it repeat and repeat to loop in the same folder and unable to proceed to the next folder path as in figure above. I have tried to debug for few days but still fail, so I decide to seek for help from the community.
I know there are many questions about looping through the folders and subfolders have been answered but it does not help since my situation is I need the coding to loop through all the listed folder path.
Any help or suggestion is appreciated, Thanks!