[FONT="]Hi Experts. I managed to find a vba to consolidate information from different workbooks into a single sheet but I need help to modify it slightly.
[/FONT][FONT="]I have X number of workbooks. In each excel workbook, I have lets say 10 sheets. This workbook is sent out to my team to fill up. [/FONT]
[FONT="]In the master file, I would like to keep the original 10 sheets headers with the data to be drawn from the workbooks submitted by the teams.[/FONT]
[FONT="]In the below code, it is able to consolidate the data from the various workbooks but it only does so for the first sheet in each workbook, I need it to repeat it for the remaining 9 sheets for example.[/FONT][FONT="] Please help me with this request! Would really appreciate the help. [/FONT]
[FONT="]Sub MergeTest()[/FONT]
[FONT="] Dim SummarySheet As Worksheet[/FONT]
[FONT="] Dim FolderPath As String[/FONT]
[FONT="] Dim SelectedFiles() As Variant[/FONT]
[FONT="] Dim NRow As Long[/FONT]
[FONT="] Dim FileName As String[/FONT]
[FONT="] Dim NFile As Long[/FONT]
[FONT="] Dim WorkBk As Workbook[/FONT]
[FONT="] Dim SourceRange As Range[/FONT]
[FONT="] Dim DestRange As Range[/FONT]
[FONT="] Dim LastRow As Long[/FONT]
[FONT="] ' Create a new workbook and set a variable to the first sheet.[/FONT]
[FONT="] Set SummarySheet = Workbooks.Add(xlWBATWorksh[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]eet).Works[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]heets(1)[/FONT]
[FONT="] ' Open the file dialog box and filter on Excel files, allowing multiple files[/FONT]
[FONT="] ' to be selected.[/FONT]
[FONT="] SelectedFiles = Application.GetOpenFilenam[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]e(filefilt[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]er:="Excel[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="] Files (*.xl*), *.xl*", MultiSelect:=True)[/FONT]
[FONT="] ' NRow keeps track of where to insert new rows in the destination workbook.[/FONT]
[FONT="] NRow = 1[/FONT]
[FONT="] ' Loop through the list of returned file names[/FONT]
[FONT="] For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)[/FONT]
[FONT="] ' Set FileName to be the current workbook file name to open.[/FONT]
[FONT="] FileName = SelectedFiles(NFile)[/FONT]
[FONT="] ' Open the current workbook.[/FONT]
[FONT="] Set WorkBk = Workbooks.Open(FileName)[/FONT]
[FONT="] ' Get row number of last used row[/FONT]
[FONT="] LastRow = WorkBk.Worksheets(1).Cells[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="].Find(What[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]:="*", _[/FONT]
[FONT="] After:=WorkBk.Worksheets(1[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]).Cells.Ra[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]nge("A1"),[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="] _[/FONT]
[FONT="] SearchDirection:=xlPreviou[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]s, _[/FONT]
[FONT="] LookIn:=xlFormulas, _[/FONT]
[FONT="] SearchOrder:=xlByRows).Row[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">
[FONT="] ' Set the cell in column N to be the file name.[/FONT]
[FONT="] SummarySheet.Range("N" & NRow).Value = FileName[/FONT]
[FONT="] [/FONT]
[FONT="] ' Create header row[/FONT]
[FONT="] Set SourceRange = WorkBk.Worksheets(1).Range[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]("A1:W1")[/FONT]
[FONT="] Set DestRange = SummarySheet.Range("A1:W1"[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="])[/FONT]
[FONT="] DestRange.Value = SourceRange.Value[/FONT]
[FONT="] ' Set the source range to be B1 through M?.[/FONT]
[FONT="] ' Modify this range for your workbooks. It can span multiple rows.[/FONT]
[FONT="] Set SourceRange = WorkBk.Worksheets(1).Range[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]("A2:W" & LastRow)[/FONT]
[FONT="] ' Set the destination range to start at column A and be the same size as the source range.[/FONT]
[FONT="] Set DestRange = SummarySheet.Range("A" & NRow)[/FONT]
[FONT="] Set DestRange = DestRange.Resize(SourceRan[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]ge.Rows.Co[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]unt, SourceRange.Columns.Count)[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">
[FONT="] ' Copy over the values from the source to the destination.[/FONT]
[FONT="] DestRange.Value = SourceRange.Value[/FONT]
[FONT="] ' Increase NRow so that we know where to copy data next.[/FONT]
[FONT="] NRow = NRow + DestRange.Rows.Count[/FONT]
[FONT="] ' Close the source workbook without saving changes.[/FONT]
[FONT="] WorkBk.Close savechanges:=False[/FONT]
[FONT="] Next NFile[/FONT]
[FONT="] ' Call AutoFit on the destination sheet so that all data is readable.[/FONT]
[FONT="] SummarySheet.Columns.AutoF[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]it[/FONT]
[FONT="]End Sub[/FONT]
[/FONT][FONT="]I have X number of workbooks. In each excel workbook, I have lets say 10 sheets. This workbook is sent out to my team to fill up. [/FONT]
[FONT="]In the master file, I would like to keep the original 10 sheets headers with the data to be drawn from the workbooks submitted by the teams.[/FONT]
[FONT="]In the below code, it is able to consolidate the data from the various workbooks but it only does so for the first sheet in each workbook, I need it to repeat it for the remaining 9 sheets for example.[/FONT][FONT="] Please help me with this request! Would really appreciate the help. [/FONT]
[FONT="]Sub MergeTest()[/FONT]
[FONT="] Dim SummarySheet As Worksheet[/FONT]
[FONT="] Dim FolderPath As String[/FONT]
[FONT="] Dim SelectedFiles() As Variant[/FONT]
[FONT="] Dim NRow As Long[/FONT]
[FONT="] Dim FileName As String[/FONT]
[FONT="] Dim NFile As Long[/FONT]
[FONT="] Dim WorkBk As Workbook[/FONT]
[FONT="] Dim SourceRange As Range[/FONT]
[FONT="] Dim DestRange As Range[/FONT]
[FONT="] Dim LastRow As Long[/FONT]
[FONT="] ' Create a new workbook and set a variable to the first sheet.[/FONT]
[FONT="] Set SummarySheet = Workbooks.Add(xlWBATWorksh[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]eet).Works[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]heets(1)[/FONT]
[FONT="] ' Open the file dialog box and filter on Excel files, allowing multiple files[/FONT]
[FONT="] ' to be selected.[/FONT]
[FONT="] SelectedFiles = Application.GetOpenFilenam[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]e(filefilt[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]er:="Excel[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="] Files (*.xl*), *.xl*", MultiSelect:=True)[/FONT]
[FONT="] ' NRow keeps track of where to insert new rows in the destination workbook.[/FONT]
[FONT="] NRow = 1[/FONT]
[FONT="] ' Loop through the list of returned file names[/FONT]
[FONT="] For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)[/FONT]
[FONT="] ' Set FileName to be the current workbook file name to open.[/FONT]
[FONT="] FileName = SelectedFiles(NFile)[/FONT]
[FONT="] ' Open the current workbook.[/FONT]
[FONT="] Set WorkBk = Workbooks.Open(FileName)[/FONT]
[FONT="] ' Get row number of last used row[/FONT]
[FONT="] LastRow = WorkBk.Worksheets(1).Cells[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="].Find(What[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]:="*", _[/FONT]
[FONT="] After:=WorkBk.Worksheets(1[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]).Cells.Ra[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]nge("A1"),[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="] _[/FONT]
[FONT="] SearchDirection:=xlPreviou[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]s, _[/FONT]
[FONT="] LookIn:=xlFormulas, _[/FONT]
[FONT="] SearchOrder:=xlByRows).Row[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">
[FONT="] ' Set the cell in column N to be the file name.[/FONT]
[FONT="] SummarySheet.Range("N" & NRow).Value = FileName[/FONT]
[FONT="] [/FONT]
[FONT="] ' Create header row[/FONT]
[FONT="] Set SourceRange = WorkBk.Worksheets(1).Range[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]("A1:W1")[/FONT]
[FONT="] Set DestRange = SummarySheet.Range("A1:W1"[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="])[/FONT]
[FONT="] DestRange.Value = SourceRange.Value[/FONT]
[FONT="] ' Set the source range to be B1 through M?.[/FONT]
[FONT="] ' Modify this range for your workbooks. It can span multiple rows.[/FONT]
[FONT="] Set SourceRange = WorkBk.Worksheets(1).Range[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]("A2:W" & LastRow)[/FONT]
[FONT="] ' Set the destination range to start at column A and be the same size as the source range.[/FONT]
[FONT="] Set DestRange = SummarySheet.Range("A" & NRow)[/FONT]
[FONT="] Set DestRange = DestRange.Resize(SourceRan[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]ge.Rows.Co[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]unt, SourceRange.Columns.Count)[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">
[FONT="] ' Copy over the values from the source to the destination.[/FONT]
[FONT="] DestRange.Value = SourceRange.Value[/FONT]
[FONT="] ' Increase NRow so that we know where to copy data next.[/FONT]
[FONT="] NRow = NRow + DestRange.Rows.Count[/FONT]
[FONT="] ' Close the source workbook without saving changes.[/FONT]
[FONT="] WorkBk.Close savechanges:=False[/FONT]
[FONT="] Next NFile[/FONT]
[FONT="] ' Call AutoFit on the destination sheet so that all data is readable.[/FONT]
[FONT="] SummarySheet.Columns.AutoF[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]it[/FONT]
[FONT="]End Sub[/FONT]