VBA Check if File is Open

VBA Check if File is Open

VBA Check if File is Open or not using Excel VBA. In general we automate reports using Excel VBA. When we are dealing with multiple workbooks. It is always better to check whether file is opened or not. After that we can use those files for anything like data analysis, preparing reports etc.

VBA Check if File is Open or not in Excel

Let us see an example to check if file is opened or not using Excel VBA. In the following example we are looping through all opened workbooks and checking specified file is opened or not.

'VBA Check if File is Open
Sub VBAF1_Check_if_File_is_Open()

    'Variable declaration
    Dim sWBName As String
    Dim oWorkbook As Workbook
    Dim bChk As Boolean
    'Define file Name
    sWBName = "test.xlsx"
    'Loop through all opened workbooks
    For Each oWorkbook In Workbooks
        If oWorkbook.Name = sWBName Then
            'If file is open
            MsgBox "File is opened.", vbInformation, "VBAF1"
            bChk = True
        End If
    'If file is not open
    If bChk = False Then MsgBox "File is not opened.", vbInformation, "VBAF1"
End Sub

Output: In the above example we have specified file name as “test.xlsx”. You can change as per your requirement. We have provided comments for set of instructions to make you understand. You can find following screenshot for your reference. It displays when file is opened.
VBA Check if File is Open

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays VBA Text Files VBA Tables

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers Blog

Leave a Comment