Dear LEM Readers,
While doing programming with VBA many a times it happens to open an existing file. Before opening that file, it is always a good idea to check if that file is already open or not. Therefore I have written a function (IsFileOpen()) which takes the complete path of that file (with file name) as an Input parameter and returns a Boolean status TRUE or FALSE as shown below:

Technique used : To find if file is already Open

There is a specific error number which is thrown by windows whenever it finds that File your are trying to open.. is already open.
There is no specific function to find out whether file is open or not.. rather you can find out by capturing the error number.
In this function first I will open the file as given in the input parameter and capture the error number. If we are trying to open and already opened file then the Number of the error occurred is 70. Based on this technique, you can find if that file is already opened.

Function IsFileOpen(fileFullName As String)
    Dim FileNumber As Integer
    Dim errorNum As Integer

    On Error Resume Next
    FileNumber = FreeFile()   ' Assign a free file number.
    ' Attempt to open the file and lock it.
    Open fileFullName For Input Lock Read As #FileNumber
    Close FileNumber       ' Close the file.
    errorNum = Err         ' Assign the Error Number which occured
    On Error GoTo 0        ' Turn error checking on.
    ' Now Check and see which error occurred and based
    ' on that you can decide whether file is already
    ' open
    Select Case errorNum
        ' No error occurred so ErroNum is Zero (0)
        ' File is NOT already open by another user.
        Case 0
         IsFileOpen = False

        ' Error number for "Permission Denied." is 70
        ' File is already opened by another user.
        Case 70
            IsFileOpen = True

        ' For any other Error occurred
        Case Else
            Error errorNum
    End Select

End Function

How to use the above function?

Copy and Paste the above code in any of the module in your excel VBA code window. Now in that workbook you can call the function IsFileOpen() from anywhere in the VBA code. Output of this function is always Boolean type either TRUE or False.

This function can be used as a Cell Formula as well. In this workbook you can use this function as an excel formula which will take the same input and return TRUE of file is open and FALSE if not.

Usage 1. Calling from VBA code

Sub Test()

'   While passing the FileName you need
'   to pass complete path of the file
'   including the file name with extension

    If IsFileOpen("C:/..../File1.xls") = True Then
        MsgBox ("File is already opened")
        MsgBox ("File is not opened yet")
    End If

End Sub

2. Using as an Excel Formula (UDF – User Defined Function)

This might be really useful custom function to check if file which your formula is looking for is open or not.

You can use this as a formula as well. Refer the below picture:

IsFileOpen Function

What is User Defined Function (UDF) ?

Microsoft Excel allows us to create our own functions, based on our own need. Such functions are called User Defined Functions” (UDFs) or Custom Function.

These functions behaves exactly like Excel’s inbuilt formulas. These are helpful in simplifying complicated calculations or string related stuffs. Sometimes, you might have felt, “I wish, Excel had a built-in formula for this calculation” !! No worry, you can create your own formula.. and through all the dirty code behind the bar 😉

4 Steps to Create UDFs

Learn How to create your own Excel formula in 4 simple Steps

Hearing about creating my own defined formula is exiting and feels like a rocket science but trust it's not. Learn here in 4 simple steps how to create your own Custom Function.
4 Steps to Create UDFs


Join over 10, 000+ Excel VBA Enthusiasts & get this FREE e-Book Now!