VBA & Macro Problems

Build an Excel Workbook from Multiple Files Using a Control Table

Aamir Naveed AkramAamir Naveed Akram Dec 2, 2026 868 views

Question details

Create a report workbook by copying sheets from multiple source files according to quantities in a control table.

Build an Excel Workbook from Multiple Files Using 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.
Before you start

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.

Solution 1Recommended

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.

1
Create the control table

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.

2
Save a macro-enabled workbook

Select File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). Only enable macros for a workbook and code you trust.

3
Add the macro

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

4
Run and review the report

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.

Copy whole worksheets with an Excel VBA macro
If the macro stops: Check the file path, exact sheet name, and quantity in the control table. A partial report may remain open; review or close it before running the macro again.
Free Microsoft Office alternative

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.

Open and edit workbooks used for multi-file reports.Compatible with common Microsoft Excel formats such as XLSX and XLS.Use a familiar spreadsheet interface when moving from Microsoft Office.Work with a free, lightweight office suite.
microsoft office alternative - wps office

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.