I have some code that opens all excel files within a folder and copies cells from these files into a master recording spreadsheet. However, I have now been told that the source files will be saved within individual sub folders within this root folder. The code works perfectly for the root folder and I have tried a couple of solutions to also get information from the files in the sub folders but cannot get it to work. Could anyone suggest how I can loop through the sub folders:
Sub MergeReturns()
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim NRow As Long
Dim FileName As String
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range
'Set the sheet where you will store the merged data
Set SummarySheet = ActiveWorkbook.Worksheets(1)
' Modify this folder path to point to the files you want to use.
FolderPath = "H:\Catherine\ARMS Triage"
' NRow keeps track of where to insert new rows in the destination workbook.
NRow = InputBox("Please enter the next blank row to copy data to")
' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(FolderPath & "*.xl*")
' Loop until Dir returns an empty string.
Do While FileName <> ""
' Open a workbook in the folder
Set WorkBk = Workbooks.Open(FolderPath & FileName)
Dim ws As Worksheet
Dim Lastrow As Long
Set ws = Worksheets("TAD Tool")
Lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'
' Set the source range, destination range and then copy
'Information about client
Set SourceRange1 = WorkBk.Worksheets(1).Range("C5")
Set DestRange1 = SummarySheet.Range("B" & NRow)
DestRange1.Value = SourceRange1.Value
'etc......etc. for each cell that needs copying
' Increase NRow so that we know where to copy data next.
NRow = NRow + 6
' Close the source workbook without saving changes.
WorkBk.Close savechanges:=False
' Use Dir to get the next file name.
FileName = Dir()
Loop
' Call AutoFit on the destination sheet so that all
' data is readable.
SummarySheet.Columns.AutoFit
'DeleteBlank
DeleteWhere
AutofillFormulae
ActiveWindow.ScrollRow = NRow - 10
End Sub
Sub MergeReturns()
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim NRow As Long
Dim FileName As String
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range
'Set the sheet where you will store the merged data
Set SummarySheet = ActiveWorkbook.Worksheets(1)
' Modify this folder path to point to the files you want to use.
FolderPath = "H:\Catherine\ARMS Triage"
' NRow keeps track of where to insert new rows in the destination workbook.
NRow = InputBox("Please enter the next blank row to copy data to")
' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(FolderPath & "*.xl*")
' Loop until Dir returns an empty string.
Do While FileName <> ""
' Open a workbook in the folder
Set WorkBk = Workbooks.Open(FolderPath & FileName)
Dim ws As Worksheet
Dim Lastrow As Long
Set ws = Worksheets("TAD Tool")
Lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'
' Set the source range, destination range and then copy
'Information about client
Set SourceRange1 = WorkBk.Worksheets(1).Range("C5")
Set DestRange1 = SummarySheet.Range("B" & NRow)
DestRange1.Value = SourceRange1.Value
'etc......etc. for each cell that needs copying
' Increase NRow so that we know where to copy data next.
NRow = NRow + 6
' Close the source workbook without saving changes.
WorkBk.Close savechanges:=False
' Use Dir to get the next file name.
FileName = Dir()
Loop
' Call AutoFit on the destination sheet so that all
' data is readable.
SummarySheet.Columns.AutoFit
'DeleteBlank
DeleteWhere
AutofillFormulae
ActiveWindow.ScrollRow = NRow - 10
End Sub