Please help, I wrote the following code expecting it to step through every workbook in a specific folder and every subfolder.
But it only found the workbook in the Folder1 subfolder, it does not find any workbooks in the route Data folder or any subfolders within the Folder1 subfolder:
But it only found the workbook in the Folder1 subfolder, it does not find any workbooks in the route Data folder or any subfolders within the Folder1 subfolder:
Code:
Dim DataFolder As String
Dim WkBook As String
Dim DataRow As Long
Dim ThisWB As Workbook
Dim NegWB As Workbook
Dim Fso As Scripting.FileSystemObject
Dim Folder As Scripting.Folder, Subfolder As Scripting.Folder, File As Scripting.File
Set ThisWB = ActiveWorkbook
Sheets("Andy").Select
DataRow = 2
Set Fso = New Scripting.FileSystemObject
DataFolder = "C:\Data"
Set Folder = Fso.GetFolder(DataFolder)
For Each Subfolder In Folder.subfolders
For Each File In Subfolder.Files
WkBook = File.Name
If Left(WkBook, 1) <> "~" And Right(WkBook, 4) = "xlsm" Then
Application.DisplayAlerts = False
Application.AskToUpdateLinks = False
Workbooks.Open (File)
Application.AskToUpdateLinks = True
Application.DisplayAlerts = True
Set NegWB = ActiveWorkbook
Sheets("NEG BRIEF").Select
ThisWB.Sheets("Andy").Cells(DataRow, 1) = WkBook
ActiveWorkbook.Close savechanges:=False
ThisWB.Activate
End If
Next
Next
Set Fso = Nothing
Set Folder = Nothing
For a test I created a Data folder and the following workbooks and subfolders
C:\Data
Data.xlsm
Folder1
Folder1.xlsm
SubFolder1
SubFolder1.xlsm
SubSub1
SubSub1.xlsm
SubFolder2
SubFolder2.xlsm
SubSub2
SubSub2.xlsm