Build an Excel Workbook from Multiple Files Using a Control Table
Question details
Create a report workbook by copying sheets from multiple source files according to quantities in a control table.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A control table identifies source files and specifies how many copies of each source sheet should be added to a report.
- Observed behavior
- The desired result is a workbook containing the specified number of sheets from each source file.
Decide whether the report needs whole worksheets or only selected data, and choose a naming rule for the output sheets. For the VBA example below, gather the full paths and sheet names of trusted source workbooks and close those workbooks before running the macro.
Copy whole worksheets with an Excel VBA macro
Use this method when each control-table row specifies a source worksheet and the number of copies needed in a new workbook.
This example copies entire worksheets and names the copies Report_1, Report_2, and so on. The new workbook also retains one blank starter sheet.
Copied formulas may retain links to source workbooks. The example does not consolidate selected ranges or create custom sheet names.
In Excel, create a worksheet named Control. Enter File Path, Sheet Name, and Quantity in cells A1, B1, and C1. Starting in row 2, enter the full path to each source workbook, the exact source sheet name, and a nonnegative whole-number quantity.
Select File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). Only enable macros for a workbook and code you trust.
Open the VBA editor with Alt+F11, select Insert > Module, and paste this code: Sub BuildReport() Dim ctl As Worksheet, report As Workbook, src As Workbook Dim lastRow As Long, r As Long, i As Long, qty As Long, n As Long Dim filePath As String, sheetName As String, value As Variant On Error GoTo Failed Set ctl = ThisWorkbook.Worksheets("Control") lastRow = ctl.Cells(ctl.Rows.Count, "A").End(xlUp).Row Set report = Workbooks.Add(xlWBATWorksheet) report.Worksheets(1).Name = "Starter" n = 1 For r = 2 To lastRow filePath = Trim(ctl.Cells(r, "A").Value) sheetName = Trim(ctl.Cells(r, "B").Value) value = ctl.Cells(r, "C").Value If Not IsNumeric(value) Then Err.Raise 5, , "Invalid quantity in row " & r If CDbl(value) < 0 Or CDbl(value) <> Fix(CDbl(value)) Then Err.Raise 5, , "Invalid quantity in row " & r qty = CLng(value) If qty > 0 Then Set src = Workbooks.Open(Filename:=filePath, ReadOnly:=True) For i = 1 To qty src.Worksheets(sheetName).Copy After:=report.Worksheets(report.Worksheets.Count) report.Worksheets(report.Worksheets.Count).Name = "Report_" & n n = n + 1 Next i src.Close SaveChanges:=False Set src = Nothing End If Next r MsgBox "Report created. Review it and save it from the new workbook." Exit Sub Failed: On Error Resume Next If Not src Is Nothing Then src.Close SaveChanges:=False MsgBox "The report stopped: " & Err.Description & ". Review the control table and any partial report." End Sub
In Excel, open Developer > Macros, select BuildReport, and choose Run. In the new workbook, review the copied sheets and formulas, delete the Starter sheet if it is no longer needed, and use File > Save As to save the report.

Combine selected data with Excel Power Query
Use Power Query when the report needs rows of similarly structured data rather than repeated copies of whole worksheets.
Work with Excel report files in WPS Spreadsheets
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for common Excel formats, making it easy to move existing report files into WPS. It does not fix Excel VBA issues; macro availability and compatibility depend on the WPS edition.

Frequently Asked Questions
What should each row of the control table contain?
For the VBA example, enter the full source workbook path in column A, the exact worksheet name in column B, and a nonnegative whole-number copy count in column C. Start the data in row 2 of the Control sheet.
Can I copy only selected columns instead of whole sheets?
Yes, but the VBA example must be changed to copy specific ranges. For similarly structured data across files, try Data > Get Data > From File > From Folder and select the needed columns in Power Query.
Why do copied formulas still refer to a source workbook?
Copied worksheets can retain external references. Review formulas and workbook links in the new report after running the macro, especially if source sheets refer to other sheets or files.
How do I run the report again when the quantities change?
Update the quantities on the Control sheet, close the source workbooks, and run BuildReport again from Developer > Macros. The macro creates a new report workbook each time rather than updating the previous one.
Can Office Scripts automate this instead of VBA?
Office Scripts may be an option for a Microsoft 365 workflow, depending on access to the source files and the automation setup. Define whether you need whole sheets or selected data and how the control table identifies each source before choosing that approach.




