Read Excel Files in Folders and Subfolders with VBA
Question details
The user needs VBA code to scan a main folder and its subfolders, read specified cells from Excel workbooks, and combine the results in a summary worksheet.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- Consolidating cell values from multiple workbooks stored in nested folders.
- Observed behavior
- The desired result is one summary row per readable workbook, without changing or saving the source files.
For the Windows-based FileSystemObject example below, save the destination workbook as an Excel Macro-Enabled Workbook (.xlsm) and create a worksheet named Summary. Confirm the source worksheet and cell addresses you want to read; the example reads B2 and D2 from each workbook's first worksheet.
Use a recursive VBA procedure to collect workbook values
Use this approach in desktop Excel for Windows to process Excel files throughout a folder tree and append their values to a summary worksheet.
FileSystemObject enumerates the files and subfolders. The folder procedure calls itself for each subfolder, while a separate procedure opens each matching workbook read-only, retrieves the values, and closes it without saving.
This example processes .xlsx, .xlsm, .xlsb, and .xls files. It skips temporary lock files and the workbook containing the macro, disables macros in workbooks opened by the procedure, and restores Excel's application settings when processing ends.
In Excel, create a worksheet named Summary. Select File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). Use a dedicated summary sheet: the macro writes headers in A1:C1 and appends records below existing entries in column A.
Press Alt+F11 to open the Visual Basic Editor, then select Insert > Module. Paste the following code into the module: Option Explicit Public Sub CollectWorkbookValues() Dim picker As Object, fso As Object Dim rootPath As String, output As Worksheet Dim nextRow As Long, startRow As Long Dim oldScreen As Boolean, oldEvents As Boolean Dim oldSecurity As Long, settingsSaved As Boolean Dim failure As String On Error GoTo Failed Set picker = Application.FileDialog(4) With picker .Title = "Select the folder containing source workbooks" .AllowMultiSelect = False If .Show <> -1 Then Exit Sub rootPath = .SelectedItems(1) End With Set output = ThisWorkbook.Worksheets("Summary") output.Range("A1").Value = "Source file" output.Range("B1").Value = "B2 value" output.Range("C1").Value = "D2 value" nextRow = output.Cells(output.Rows.Count, 1).End(xlUp).Row + 1 If nextRow < 2 Then nextRow = 2 startRow = nextRow oldScreen = Application.ScreenUpdating oldEvents = Application.EnableEvents oldSecurity = Application.AutomationSecurity settingsSaved = True Application.ScreenUpdating = False Application.EnableEvents = False Application.AutomationSecurity = 3 Set fso = CreateObject("Scripting.FileSystemObject") ScanFolder fso.GetFolder(rootPath), fso, output, nextRow GoTo Cleanup Failed: failure = Err.Description Cleanup: If settingsSaved Then Application.AutomationSecurity = oldSecurity Application.EnableEvents = oldEvents Application.ScreenUpdating = oldScreen End If If Len(failure) > 0 Then MsgBox "Processing stopped: " & failure, vbExclamation Else MsgBox CStr(nextRow - startRow) & " workbook(s) added. See the Immediate Window for skipped files.", vbInformation End If End Sub Private Sub ScanFolder(ByVal folder As Object, ByVal fso As Object, _ ByVal output As Worksheet, ByRef nextRow As Long) Dim file As Object, child As Object Dim extension As String For Each file In folder.Files extension = LCase$(fso.GetExtensionName(file.Name)) Select Case extension Case "xlsx", "xlsm", "xlsb", "xls" If Left$(file.Name, 2) <> "~$" And _ StrComp(file.Path, ThisWorkbook.FullName, vbTextCompare) <> 0 Then ReadOneWorkbook file.Path, output, nextRow End If End Select Next file For Each child In folder.SubFolders ScanFolder child, fso, output, nextRow Next child End Sub Private Sub ReadOneWorkbook(ByVal filePath As String, _ ByVal output As Worksheet, ByRef nextRow As Long) Dim source As Workbook, sheet As Worksheet Dim valueB As Variant, valueD As Variant On Error GoTo FileFailed Set source = Workbooks.Open(Filename:=filePath, UpdateLinks:=0, _ ReadOnly:=True, IgnoreReadOnlyRecommended:=True, _ AddToMru:=False) Set sheet = source.Worksheets(1) valueB = sheet.Range("B2").Value2 valueD = sheet.Range("D2").Value2 output.Cells(nextRow, 1).Value2 = filePath output.Cells(nextRow, 2).Value2 = valueB output.Cells(nextRow, 3).Value2 = valueD nextRow = nextRow + 1 GoTo CloseSource FileFailed: Debug.Print "Skipped: " & filePath & " | " & Err.Description CloseSource: On Error Resume Next If Not source Is Nothing Then source.Close SaveChanges:=False On Error GoTo 0 End Sub
In ReadOneWorkbook, replace source.Worksheets(1) with source.Worksheets("Data") if your source sheet is named Data. Replace Range("B2") and Range("D2") with the required addresses, and update the summary headers in CollectWorkbookValues to match.
Close the source workbooks before running the macro. Return to Excel, press Alt+F8, select CollectWorkbookValues, and click Run. Choose the main folder, then check the Summary sheet. In the Visual Basic Editor, press Ctrl+G to review skipped-file messages in the Immediate Window.
Review and share your consolidated workbook with WPS Spreadsheet
WPS Office is a free, lightweight alternative for opening, editing, and sharing the summary workbook after consolidation. Its familiar spreadsheet interface and support for common Excel formats make migration straightforward. It does not fix Excel VBA issues, and support for this Windows-specific macro depends on the WPS edition and environment.

Frequently Asked Questions
Do I need to enable the Microsoft Scripting Runtime reference?
No. The example uses CreateObject("Scripting.FileSystemObject"), which is late binding. Paste it into a standard module using Alt+F11 > Insert > Module; you do not need to select Microsoft Scripting Runtime under Tools > References.
How can I read the same named worksheet in every workbook?
In ReadOneWorkbook, change Set sheet = source.Worksheets(1) to Set sheet = source.Worksheets("Data"), replacing Data with your sheet name. Workbooks that do not contain that worksheet will be skipped and logged in the Visual Basic Editor's Immediate Window, available with Ctrl+G.
Will this macro change or save the source workbooks?
The code opens each source with ReadOnly:=True and closes it with SaveChanges:=False. It writes collected values only to the Summary sheet in the macro workbook. Close source workbooks before running it to avoid interfering with workbooks already open in Excel.
Why are some files missing from the summary?
The scan only includes .xlsx, .xlsm, .xlsb, and .xls files, and it excludes temporary files beginning with ~$ and the macro workbook itself. Open the Visual Basic Editor and press Ctrl+G to see errors for skipped workbooks, such as missing worksheets or files that could not be opened.
Can I run this recursive scan in Excel for the web or WPS Spreadsheet?
Excel for the web cannot run VBA macros; use desktop Excel for Windows for this FileSystemObject example. WPS VBA availability varies by edition and environment, so do not assume the code will run there. You can instead open the resulting .xlsx summary in WPS Spreadsheet for review and editing.




