To check whether a workbook is open in Excel VBA, loop through Application.Workbooks and compare each open workbook’s name or full path with the one you want. The function below returns True if it finds a match and False otherwise.
Public Function IsWorkbookOpen(ByVal workbookID As String) As Boolean
Dim wb As Workbook
If Len(Trim$(workbookID)) = 0 Then Exit Function
For Each wb In Application.Workbooks
If StrComp(wb.Name, workbookID, vbTextCompare) = 0 _
Or StrComp(wb.FullName, workbookID, vbTextCompare) = 0 Then
IsWorkbookOpen = True
Exit Function
End If
Next wb
End Function
Pass either a workbook filename, such as Sales.xlsx, or its full path, such as C:\Reports\Sales.xlsx. A filename check answers whether a workbook with that name is open; a full-path check identifies a particular saved file. The comparison is case-insensitive.
How the Workbooks collection works
Application.Workbooks is Excel’s collection of workbooks open in the current Excel application instance. VBA can examine its members one at a time using For Each. Each member is a Workbook object, with properties such as Name (the displayed file name) and FullName (the file name together with its path).
The function does not search folders or inspect files on disk. It asks whether a matching workbook is already loaded in that instance of Excel. That is useful before opening a file, before running code that expects a workbook to be available, or before deciding whether to alert a user.
It also avoids depending on which workbook is selected. ActiveWorkbook identifies the workbook currently in front, not every open workbook. A target can be open but inactive, so checking only ActiveWorkbook.Name can incorrectly report that it is absent.
Call the function from a macro
Once the function is in a standard VBA module, another macro can use its Boolean result in an If statement. For example, with a workbook named Sales.xlsx expected to be open, use:
Sub CheckSalesWorkbook()
Const targetName As String = "Sales.xlsx"
If IsWorkbookOpen(targetName) Then
MsgBox targetName & " is already open."
Else
MsgBox targetName & " is not open in this Excel instance."
End If
End Sub
This check only reports status; it does not open, close, save, or change the workbook. If the workbook is open and the next step needs to work with it, retrieve a reference to the matching Workbook object rather than assuming the target is active. You can do that by using the same loop and assigning the matching item to a variable declared As Workbook.
For a workflow that should open the file when it is absent, put the opening logic in the Else branch. Check the path and handle any error from opening it: a file can be missing, inaccessible, or blocked even though the open-workbook check correctly returns False.
Choose a filename or full path
Use Name when the question is simply whether a workbook with a particular filename is open. For example, IsWorkbookOpen("Budget.xlsx") can match a workbook stored in any folder if that file name appears in the collection. This is convenient when a person might open the file from different locations.
Use the full path when the location matters. Two files in different folders can share a filename, such as C:\Team A\Status.xlsx and C:\Team B\Status.xlsx. Checking only Status.xlsx cannot tell you which copy matched. Comparing FullName with a complete path makes the intended file explicit. Make sure the path you pass reflects the actual saved location.
An unsaved workbook has no ordinary saved-file path to use as a dependable disk identity. Its Name is still available, typically with a default name such as Book1. If your code cares about a particular unsaved workbook, use a reference to the workbook object or define a separate way to identify it rather than treating a disk path as its identity.
Common approaches and their limitations
A compact alternative is to try Application.Workbooks(workbookName) and use error handling to catch the error raised when the item cannot be found. It can be useful for a quick check by workbook name, but it relies on collection lookup behavior and often uses On Error Resume Next. If error handling is not reset immediately, unrelated errors later in the procedure can be hidden. The loop makes the comparison criteria visible and is easy to adapt to a full-path check.
Another common mistake is using Dir to answer whether a workbook is open. Dir checks for a matching file in the file system; it does not tell you whether Excel currently has that file open. A file can exist and be closed, or be open after it has been moved or renamed on disk. Decide whether the question concerns an open workbook or a file’s existence, then use the corresponding check.
Likewise, checking Application.Workbooks.Count only tells you how many workbooks are open. It does not establish whether one particular target is among them. The loop is what connects the collection to the specific name or path you care about.
Excel instances and platform limits
The function searches only the Excel application instance represented by Application where the code is running. If a person has launched a separate Excel instance and opened the target there, this loop will not find it. That distinction matters in automated workflows: two Excel windows do not always mean the windows share the same application instance.
VBA runs in the desktop versions of Excel, including Microsoft 365 for Windows and Excel for Mac. Excel for the web does not run VBA macros. On Windows, Alt+F11 opens the Visual Basic Editor; on Mac, the shortcut is commonly Option+F11, with Fn also needed on some keyboards. Menu availability and exact keyboard behavior can vary by Mac setup.
On Mac, the same collection-based approach applies, but full paths use the path format reported by that Excel installation. If matching a full path, compare against the workbook’s actual FullName value rather than assuming a Windows-style path. The sample uses a case-insensitive comparison, which is suitable for ordinary Excel workbook-name checks; specialized file systems can have different path-case rules.
Adding and running the code safely
To add the function in desktop Excel, open the Visual Basic Editor, choose Insert > Module, and paste the function into the new standard module. Add the calling macro there as well, or in another standard module. A procedure placed in a worksheet or workbook code module has a different role; a standard module is the straightforward place for a reusable public function.
Save a workbook containing VBA in a macro-enabled format such as .xlsm, and allow macros only when the file and its source are trusted. Before running a macro, save a copy of the workbook. Although the sample check itself does not edit workbook data, later actions added to the macro might; VBA changes cannot be reversed with Ctrl+Z (on Mac, Command+Z).
To test the function, open a workbook with a known name, call the function with that name, and then try a name that is not open. You can also test a full path using the value shown by the open workbook’s FullName property. That verifies both the match and the identifier format before the check becomes part of a larger process.
When to use a different check
If the goal is to find out whether a file exists before trying to open it, use a file-system check rather than interpreting False from this function as proof that the file is missing. “Not open” and “does not exist” are different conditions. A robust automation sequence can check whether the workbook is already open, then check whether the file exists, then open it and handle any access or format errors.
If the goal is to reuse an already-open workbook, a function that returns the matching Workbook object can be more useful than a Boolean function. It combines finding the workbook with obtaining the reference needed for later code. For a simple yes-or-no decision, however, the Boolean function keeps the intent clear and avoids returning an object the caller does not need.
For a user-facing task where macros are not appropriate, Excel’s interface can show open workbooks, but that is a manual check rather than a reliable automated condition. Formulas in worksheet cells also cannot inspect Excel’s open-workbook collection. VBA is the suitable option when the check must be performed by code in desktop Excel.
Frequently Asked Questions
Does this check whether a workbook is open in another Excel window?
It checks the Excel application instance running the macro. A workbook open in a separate Excel instance may not appear in that instance’s Application.Workbooks collection.
Can I check by the workbook’s full path?
Yes. Pass the expected full path to the function. It compares that value with each open workbook’s FullName, so the check can distinguish files with the same filename in different folders.
Does a False result mean the file does not exist?
No. It means the function did not find a matching open workbook in the current instance. The file may exist on disk but be closed; use a separate file-existence check if that is what you need to know.
Can I use this in Excel for the web?
No. Excel for the web does not run VBA. Run the macro in desktop Excel for Windows or Mac, with macros enabled for the workbook.
Why not just check ActiveWorkbook?ActiveWorkbook refers to the workbook currently active in the Excel window. It does not search all open workbooks, so it can miss the target when another workbook is in front.
Related
- Excel Not Responding? Causes and Fixes
- Excel Not Responding? Recover Files and Find Temp Data
- Excel Not Responding in Safe Mode? Causes and Fixes
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Delete Row Based on Cell Value in ExcelTo delete Excel rows when a cell contains a chosen value, loop through the worksheet from the bottom…
- VBA Range Properties: Values, Formatting, and NavigationIn Excel VBA, a Range property either returns information about cells or another range object, or changes a…
- Access VBA Find Text: Methods, Examples, and FixesTo find text in Access VBA, use a DAO recordset’s FindFirst method with a criterion such as [LastName]…
- VBA Copy and Paste Values Only: Practical Methods and CodeTo copy values only with VBA, assign the source range’s values to an equally sized destination range: destinationRange.Value…
- VBA Loop Over a Range: Methods, Examples, and TipsTo loop over cells in an Excel range with VBA, use For Each cell In someRange when you…
- Excel VBA UserForm Parameters: Pass and Return ValuesTo pass parameters to an Excel VBA UserForm, create an instance of the form, call a public setup…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.