Combine Excel Worksheets and Repeat Cell Values with Power Query
Question details
Combine identically structured worksheets from multiple workbooks and repeat each worksheet’s B1 and B2 values on its imported rows.

- Product
- Microsoft Excel Power Query
- Device & OS
- not provided
- Scenario
- Excel workbooks are stored in multiple folders, and each relevant worksheet has the same data layout but different values in B1 and B2.
- Observed behavior
- The desired result is one appended table with worksheet-specific cell values, folder paths, workbook names, and sheet names on each data row.
Place the source workbooks under one parent folder and confirm that the relevant sheets share the same layout. The example assumes B1 and B2 contain the values to repeat and row 3 contains the table headers; adjust the row offset if needed.
Combine folder workbooks with a Power Query script
Use this approach when the worksheets have a consistent layout and you want to refresh the combined table as files are added.
Folder.Files includes files in subfolders. The query reads every worksheet in each matching workbook, so keep unrelated workbooks and sheets out of the source folder or add filters for them.
The example creates columns named SourceFolder, SourceFile, SourceSheet, CellB1, and CellB2. Make sure the source headers do not use those names.
In Excel, select Data > Get Data > From Other Sources > Blank Query. In the Power Query Editor, select Home > Advanced Editor.
Replace the existing code with the following, then replace C:\YourParentFolder with your folder path: let Files = Folder.Files("C:\YourParentFolder"), SourceFiles = Table.SelectRows(Files, each List.Contains({".xlsx", ".xlsm"}, Text.Lower([Extension])) and not Text.StartsWith([Name], "~$")), PerFile = Table.AddColumn(SourceFiles, "CombinedSheets", (f) => let Workbook = Excel.Workbook(f[Content], null, true), Sheets = Table.SelectRows(Workbook, each [Kind] = "Sheet" and [Hidden] <> true), Outputs = Table.AddColumn(Sheets, "Output", (s) => let Raw = s[Data], B1 = try Raw{0}[Column2] otherwise null, B2 = try Raw{1}[Column2] otherwise null, Headers = Table.PromoteHeaders(Table.Skip(Raw, 2), [PromoteAllScalars = true]), AddFolder = Table.AddColumn(Headers, "SourceFolder", each f[Folder Path]), AddFile = Table.AddColumn(AddFolder, "SourceFile", each f[Name]), AddSheet = Table.AddColumn(AddFile, "SourceSheet", each s[Item]), AddB1 = Table.AddColumn(AddSheet, "CellB1", each B1), AddB2 = Table.AddColumn(AddB1, "CellB2", each B2) in AddB2), Combined = Table.Combine(Outputs[Output]) in Combined), Result = Table.Combine(PerFile[CombinedSheets]) in Result
Select Done and inspect the preview. If headers are not on row 3, change the 2 in Table.Skip(Raw, 2) to the number of rows before the header. Filter unwanted rows or sheets in the query as needed.
Select Home > Close & Load. After adding or changing source files, use Data > Refresh All to update the combined table.

Work with combined Excel tables in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and Excel format compatibility, making it easy to move and work with workbook data. It does not replace this Microsoft Excel Power Query workflow.

Frequently Asked Questions
Will the query include files in subfolders?
Yes. Folder.Files reads files in the specified folder and its subfolders. The example then filters for XLSX and XLSM files.
How do I change which row becomes the header?
In Home > Advanced Editor, change Table.Skip(Raw, 2) to skip the number of rows before your header. For example, use 4 if the headers are on row 5.
How are B1 and B2 repeated for each sheet?
The query reads B1 and B2 from each sheet before promoting headers, then adds their values as CellB1 and CellB2 columns to every data row from that sheet.
How do I include new workbooks later?
Add matching workbooks under the source folder, then select Data > Refresh All in Excel. Their relevant sheets must follow the same layout.
What if a workbook contains sheets I do not want to combine?
The example imports all visible worksheets. Keep unrelated sheets out of the source workbooks or modify the Sheets filter in Advanced Editor to select only the required sheet names.




