30 Advanced Excel Tips for 2026

Excel Champs / VBA / VBA Check IF a Workbook is Open (Excel File)
By Puneet Gogia (Microsoft MVP)

VBA Check IF a Workbook is Open (Excel File)

30+ Advanced Excel Tips for 2026

No spam. Only Excel Stuff. God Promise. 😊

Loop through the open workbooks and compare names. StrComp with vbTextCompare ignores upper and lower case:

Function IsWorkbookOpen(fileName As String) As Boolean
    Dim wb As Workbook
    For Each wb In Workbooks
        If StrComp(wb.Name, fileName, vbTextCompare) = 0 Then
            IsWorkbookOpen = True
            Exit Function
        End If
    Next wb
End Function

Call it with the full file name, including the extension: If IsWorkbookOpen("Sales.xlsx") Then Workbooks("Sales.xlsx").Activate. Without the extension, the names don’t match and the function returns False.

It checks only the copy of Excel the code is running in. A workbook opened in a separately started Excel window, or by another user on a shared drive, won’t show up. To find out whether a file is in use by anyone, try to open it for exclusive access:

Function IsFileInUse(filePath As String) As Boolean
    Dim f As Integer
    f = FreeFile
    On Error Resume Next
    Open filePath For Binary Access Read Write Lock Read Write As #f
    IsFileInUse = (Err.Number <> 0)
    Close #f
    On Error GoTo 0
End Function

Check that the file exists with Dir before calling it, because opening a missing file this way creates an empty one.

To check if a workbook is open using a VBA code, you need to use a FOR EACH loop that can loop through all the workbooks that are open at the moment and verify each workbook’s name with the name you have mentioned. You can use a message box to get the result of the loop. Or you can also make the code enter the result in a cell.

Check IF a WORKBOOK is OPEN

  1. First, you need to declare variables to use in the code to create a loop.
    create a loop
  2. Use an input box to get the name of the workbook that you wish to search for.
    use an input box
  3. Start the loop to loop through all the open workbooks.
    start the loop to loop
  4. Write code with IF STATEMENT to verify the name of the workbook with the name you have entered in the input box, and once the name matches, activate the workbook, show a message box that the workbook is found, and exit the procedure.
    code with if statement
  5. In the end, end the loop and use a message box to show a message if nothing has been found.
    end the loop and use a message box

Helpful Links: Run a Macro – Macro Recorder – Visual Basic Editor – Personal Macro Workbook

Here’s the full code.

Sub vba_check_workbook()

Dim WB As Workbook
Dim myWB As String

myWB = InputBox(Prompt:="Enter the workbook name.")

For Each WB In Workbooks
    If WB.Name = myWB Then
        WB.Activate
        MsgBox "Workbook Found!"
        Exit Sub
    End If
Next WB

MsgBox "Not Found"

End Sub

There are two things to keep in mind with this code. First, the name of a saved workbook includes its extension, so you need to enter “Book1.xlsx”, not “Book1”. Second, the comparison is case-sensitive, so “book1.xlsx” won’t match “Book1.xlsx”. To ignore the case, compare both names in lowercase: If LCase(WB.Name) = LCase(myWB) Then.

A Function to Check IF a Workbook Is Open

If you need this check in more than one macro, put it in a custom function that returns True or False. It tries to refer to the workbook by its name, and if that gives an error, the workbook isn’t open. Put it in a standard module:

Function IsWorkbookOpen(wbName As String) As Boolean
Dim wb As Workbook
On Error Resume Next
Set wb = Workbooks(wbName)
On Error GoTo 0
IsWorkbookOpen = Not wb Is Nothing
End Function

Sub test_workbook_open()
If IsWorkbookOpen("Book1.xlsx") Then
MsgBox "The workbook is open."
Else
MsgBox "The workbook is not open."
End If
End Sub

Check IF a Workbook Is Open by Another User

The codes above only check the workbooks open in your own Excel. To find out if a file on a shared folder is in use by someone else, try to open the file for writing with a lock. If that fails, the file is in use:

Function IsFileInUse(filePath As String) As Boolean
Dim fileNum As Integer
fileNum = FreeFile
On Error Resume Next
Open filePath For Binary Access Read Write Lock Read Write As #fileNum
IsFileInUse = (Err.Number <> 0)
Close #fileNum
On Error GoTo 0
End Function

Call it with the full path, like IsFileInUse(“C:\Data\Sales.xlsx”). Note that it also returns True when the path is wrong, so check that the file exists first.

Frequently Asked Questions

Why does the code say “Not Found” when the workbook is open?

Usually, the name doesn’t match exactly. Include the file extension for a saved workbook, check the case of the letters, and check for extra spaces.

Does this check workbooks in another Excel instance?

No. The Workbooks collection only has the workbooks of the Excel instance that runs the code. A workbook opened in a separate instance of Excel isn’t in it; use the file-in-use check above for that case.

How do I open the workbook if it isn’t open?

Use the function above, and when it returns False, open the file with Workbooks.Open and the full path of the file.

Puneet Gogia

Written by

Puneet Gogia

Microsoft MVP — Excel · Founder, Excel Champs

Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.

3M+ readers helped
1,000+ tutorials written
Since 2015 teaching Excel