How to Check If an Excel File Is Open Using Excel VBA
When automating Excel tasks, you may need to check if an Excel file is already open before running macros. Attempting to open a file that’s already open can cause errors, data corruption, or unexpected results.
This guide will show you a simple VBA function to safely check if a workbook is open, whether in your current Excel session or another instance of Excel.
Why You Should Check If an Excel File Is Open
Opening a workbook that’s already open can cause runtime errors or prevent updates from being applied correctly. In some cases, it may even lead to data loss or file corruption.
Checking whether a file is open before working with it helps you:
- Prevent runtime errors in VBA macros.
- Protect data from accidental overwrites.
- Improve reliability in automated reports and workflows.
- Safely handle shared workbooks in multi-user environments.
This small validation step can significantly improve the stability of your Excel automation.
VBA Function to Check If an Excel File Is Open
The following function checks whether a workbook is open in any Excel instance. It first checks the current session, then tests if the file is locked by another process.
Option Explicit
Function IsExcelFileOpen(ByVal filePath As String) As Boolean
Dim wb As Workbook
Dim fileNumber As Integer
Dim errNumber As Integer
' File must exist to be considered open
If Len(Dir$(filePath, vbNormal)) = 0 Then
IsExcelFileOpen = False
Exit Function
End If
' Check if workbook is open in this Excel instance
For Each wb In Application.Workbooks
If StrComp(wb.FullName, filePath, vbTextCompare) = 0 Then
IsExcelFileOpen = True
Exit Function
End If
Next wb
' Check if file is open in another Excel instance or locked
On Error Resume Next
fileNumber = FreeFile
Open filePath For Binary Access Read Write _
Lock Read Write As #fileNumber
Close #fileNumber
errNumber = Err.Number
On Error GoTo 0
IsExcelFileOpen = (errNumber <> 0)
End Function
NOTE:
filePathshould include the full path to the Excel file.- The
IsExcelFileOpenfunction returnsTrueif the file is open andFalseif it is not.- Only a simple file existence check is performed in this function. For a more comprehensive check, consider using this alternative Excel VBA function to check if a file exists.
How to Install the VBA Function
- From the
Developerribbon in Excel, clickVisual Basic. - In the
Visual Basic for Applicationswindow, go toInsert>Module. - Paste the
IsExcelFileOpenfunction into the new module. - Close the editor and return to Excel.
The function is now available to use in any macro within the workbook.
How to Use the VBA Function
Here’s a simple example that demonstrates how to use the function in a test macro:
Option Explicit
Sub TestExcelFileOpen()
Dim filePath As String
filePath = "C:\temp\test.xlsx"
If IsExcelFileOpen(filePath) Then
MsgBox "The Excel file is already open!"
Else
MsgBox "The Excel file is not open."
End If
End Sub
Results
- If the file is open in the current Excel instance, the message
The Excel file is already open!is displayed. - If the file is open in another Excel instance, the same message is displayed.
- If the file is not open, the message
The Excel file is not open.is displayed.
By handling these cases, your macros can avoid errors and respond more intelligently.
Known Limitations
The IsExcelFileOpen function is a best-effort approach. Be aware of the following limitations:
- No URL support: Only works with file-system paths (local drives, UNC paths, or mapped network drives). It cannot detect SharePoint or OneDrive URLs.
- Indirect detection of other Excel instances: The function uses a file lock test to detect other instances. While this works in many cases, it is not foolproof. False positives can occur if another application temporarily locks the file, and false negatives can occur if the other Excel instance opens the file read-only or in shared mode.
- Locks do not always indicate Excel usage: Any process that locks the file, e.g., antivirus, backup, indexing, or restricted permissions, may trigger a
Trueresult. - Exact path required: Differences in mapped drives, UNC paths, or short (8.3) vs. long paths may prevent detection.
- Best-effort and transient: File state can change between the existence check and lock test, leading to race-conditions.
- Boolean results simplify state:
Truemeans the file is open or locked but does not specify whether it is open in this instance, elsewhere, or blocked by another process.
This function works well for local or network files in controlled environments but should not be considered fully multi-user safe, especially for cloud or SharePoint files.
Summary
Checking whether an Excel file is already open is a simple but essential step in reliable VBA automation. The IsExcelFileOpen function helps prevent errors, protects your data, and ensures your macros behave predictably across Excel instances. Adding this check to your workflows will make your automation safer and more professional.