John Dalesandro

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:

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:

  • filePath should include the full path to the Excel file.
  • The IsExcelFileOpen function returns True if the file is open and False if 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

  1. From the Developer ribbon in Excel, click Visual Basic.
  2. In the Visual Basic for Applications window, go to Insert > Module.
  3. Paste the IsExcelFileOpen function into the new module.
  4. 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

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:

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.