EliseFreedman
Programmer
Hi There
I have created two reports for each manager. They come from separate systems so are in separate workbooks. All the Reports are in the same folder. They are listed by Manager Name. So for example in the folder I have got a report PersonA_InspectionStatus and a report PersonA_IncidentStatus. Then There is a report PersonB_InspectionStatus and a report PersonB_IncidentStatus. I want to end up with a report PersonA_ActionStatus which will consist of the spreadsheet from PersonA_InspectionStatus and the Spreadsheet from PersonA_IncidentStatus. Same for Person B, C etc.
So far I have got the code below but im not sure firstly how to list all the names in the report without having to hardcode them and also how to save the mergedreport with the persons name in the title.
Can anyone help?
I have created two reports for each manager. They come from separate systems so are in separate workbooks. All the Reports are in the same folder. They are listed by Manager Name. So for example in the folder I have got a report PersonA_InspectionStatus and a report PersonA_IncidentStatus. Then There is a report PersonB_InspectionStatus and a report PersonB_IncidentStatus. I want to end up with a report PersonA_ActionStatus which will consist of the spreadsheet from PersonA_InspectionStatus and the Spreadsheet from PersonA_IncidentStatus. Same for Person B, C etc.
So far I have got the code below but im not sure firstly how to list all the names in the report without having to hardcode them and also how to save the mergedreport with the persons name in the title.
Can anyone help?
Code:
Sub combineFilesbyManager4()
Dim wb As Workbook, Nwb As Workbook, sh As Worksheet, nmAry As Variant, fName As String, fPath As String
Set sh = ThisWorkbook.Sheets(1) 'Edit sheet name
nmAry = Array("PersonA", "PersonB", "PersonC")
fPath = ThisWorkbook.Path 'Edit path if other workbooks are not in same directory as master.
If Right(fPath, 1) <> "\" Then fPath = fPath & "\"
For i = LBound(nmAry) To UBound(nmAry)
Set Nwb = Workbooks.Add
fName = Dir(fPath & "*.xl*")
Do
If InStr(fName, "_") <> 0 Then
If Left(fName, InStr(fName, "_") - 1) = nmAry(i) Then
Set wb = Workbooks.Open(fPath & fName)
Set sh = wb.Sheets(1)
sh.Copy After:=Nwb.Sheets(Nwb.Sheets.Count)
ActiveSheet.Name = Left(wb.Name, Len(wb.Name) - 5)
wb.Close False
End If
End If
fName = Dir
Loop While fName <> ""
Nwb.SaveAs Left(fName, InStr(fName, "_") - 1) & ".xlsx"
Nwb.Close False
Next
End Sub