To check all open Excel workbooks using VBA, you loop through the Workbooks collection in the Excel Application object. No external tools are required—just Excel with VBA enabled and a basic understanding of VBA syntax. This technique lets you list, inspect, or process every currently open workbook programmatically, such as for auditing, batch processing, or error checking.
What You'll Need
- Microsoft Excel (2010 or newer; works in Microsoft 365)
- VBA enabled (macro settings must allow running macros)
- At least one Excel workbook open
- Access to the Visual Basic Editor (VBE) via Alt + F11
Step-by-Step
- Open Excel and ensure all workbooks you want to check are already open. VBA cannot detect workbooks that haven’t been opened yet.
- Press Alt + F11 to open the Visual Basic Editor (VBE).
- In the VBE, go to Insert > Module to create a new standard module. A blank code window will appear on the right.
- Enter the following VBA code into the module. This example lists all open workbook names in a message box and writes them to a new worksheet named "WorkbookList":
Sub ListAllOpenWorkbooks() Dim wb As Workbook Dim ws As Worksheet Dim row As Long ' Turn off screen updating for speed Application.ScreenUpdating = False ' Create a new worksheet to list workbooks On Error Resume Next Set ws = ThisWorkbook.Worksheets("WorkbookList") If ws Is Nothing Then Set ws = ThisWorkbook.Worksheets.Add ws.Name = "WorkbookList" Else ws.Cells.Clear End If On Error GoTo 0 ' Add headers ws.Range("A1").Value = "Workbook Name" ws.Range("B1").Value = "Full Path" ws.Range("A1:B1").Font.Bold = True row = 2 ' Loop through all open workbooks For Each wb In Application.Workbooks ws.Cells(row, 1).Value = wb.Name ws.Cells(row, 2).Value = wb.FullName row = row + 1 Next wb ' Auto-fit columns for readability ws.Columns("A:B").AutoFit ' Re-enable screen updating Application.ScreenUpdating = True ' Optional: Show summary in a message box MsgBox "Found " & Application.Workbooks.Count & " open workbook(s). List written to 'WorkbookList'.", vbInformation End Sub - Press F5 while in the VBE, or return to Excel and run the macro via Developer tab > Macros > select
ListAllOpenWorkbooks> Run. - Verify the new "WorkbookList" worksheet appears in your current workbook (
ThisWorkbook), listing each open workbook’s name and full file path.
Common Mistakes
- Using
ActiveWorkbookinstead ofApplication.Workbooks:ActiveWorkbookonly refers to the currently focused workbook—not all open ones. Always iterate throughApplication.Workbooksto capture every open file. - Running code from a workbook not in
Workbookscollection: If you run the macro from an Excel add-in or personal macro workbook (PERSONAL.XLSB), it still works, but ensure you referenceThisWorkbookcorrectly if writing output there. - Forgetting to enable macros: If Excel opens in Protected View or macro security is set to "Disable all macros without notification", the code won’t run. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select "Enable all macros" (only if you trust the source).
- Attempting to modify
Workbookscollection during iteration: Adding or closing workbooks while looping can cause errors. If you need to close workbooks, collect names first, then close in a second loop.
Tips
- Use
Workbooks.Countfor quick validation: Before looping, checkApplication.Workbooks.Countto confirm at least one workbook is open—prevents unnecessary processing. - Filter by workbook properties: Inside the loop, add conditions like
If wb.Saved = False Then ...to find unsaved workbooks, orLeft(wb.Name, 1) = "["to detect add-ins (which often appear as[Analysis.xlam]). - Handle hidden workbooks: Some workbooks open hidden (e.g., via
Application.Visible = False). They still appear inWorkbooks, so usewb.Windows(1).Visibleif visibility matters. - Use
On Error Resume Nextcarefully: When accessingwb.FullName, unsaved workbooks may return an empty path—handle this case explicitly if needed. - Save your workbook as .xlsm: To retain macros permanently, save the host workbook as Excel Macro-Enabled Workbook (*.xlsm).
Troubleshooting
If your code doesn’t behave as expected, follow these steps:
- Check if the macro actually runs: Press Alt + F8 in Excel, confirm the macro appears in the list, and run it. If missing, it may be saved in the wrong workbook (e.g., in
PERSONAL.XLSBinstead of your target file). - Verify open workbooks: In the VBE Immediate Window (Ctrl + G), type
?Application.Workbooks.Countand press Enter. This returns the number of open workbooks. If it’s zero, open at least one file before running the macro. - Review error messages: If you see "Object variable or With block variable not set", ensure the code uses
For Each wb In Application.Workbooks(notWorkbooksalone, which requires an object reference). - Check for read-only or shared workbooks: Shared workbooks or those opened read-only may restrict access. Try closing and reopening them normally before running the macro.
- Reset macro environment: Close all Excel instances, then reopen Excel and the workbooks. If issues persist, temporarily disable add-ins via File > Options > Add-ins > manage "Excel Add-ins" > Go... > uncheck all > OK.
Frequently Asked Questions
Can I check workbooks in other Excel instances (e.g., a second Excel window)?
No. VBA code runs within a single Excel instance and can only access workbooks open in that instance. Workbooks opened in a separate Excel process (e.g., launching Excel twice) are invisible to Application.Workbooks. To manage workbooks across instances, you’d need external automation (e.g., PowerShell or .NET), which is beyond basic VBA.
Does this work with workbooks opened from a network drive or SharePoint?
Yes. The Workbooks collection includes all workbooks open in the instance, regardless of location (local, network, OneDrive, SharePoint). The FullName property will show the full UNC path or URL.
How do I avoid overwriting the "WorkbookList" sheet if it already exists?
The sample code handles this: it checks for a sheet named "WorkbookList" and clears it if found. If you prefer to keep existing data, modify the code to append rows instead of clearing, or use a unique sheet name (e.g., "WorkbookList_" & Format(Now, "yyyymmdd_hhmmss")).
Can I list only specific workbooks (e.g., exclude the workbook running the macro)?
Yes. Inside the loop, add a condition: If wb.Name <> ThisWorkbook.Name Then .... This skips the host workbook. Replace ThisWorkbook with another workbook’s name if you need to exclude a different one.
Why does Workbooks.Count sometimes show a higher number than expected?
Hidden or add-in workbooks (e.g., PERSONAL.XLSB, Analysis ToolPak) remain in the collection even when not visible. Use wb.HasVBProject or check wb.Type (e.g., xlWorkbookNormal = -4143) to filter non-add-in workbooks.
Is this compatible with Excel for Mac?
Yes, but with caveats. Excel for Mac requires the same Application.Workbooks loop, though some path properties (like FullName) may format paths differently (e.g., colon separators instead of backslashes). Always test on the target platform.