Testing if a Workbook is Open

Written by Allen Wyatt (last updated March 30, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


1

Brian has a complex workbook that opens other supporting workbooks very briefly and then closes them, saving changes. He has an office with three people that use his macros, and on occasion they get a conflict where two people are trying to get a purchase order number at the same time. This causes the PO workbook to open in 'read only' mode for the second occurrence, which is confusing to users. Brian wonders if there is a line of VBA code that will stop the 'open workbook' command if the target workbook is already open by a different user. That way he could catch potential problems before they occur.

It is much easier to have your code, after opening, check to see if the workbook opened in read-only mode. If it did, then you can take an action appropriate to your situation. (For instance, you could close the workbook, wait a short period, and retry the operation and test.) Here's how you can check to see the read-only status:

Set wkBook1 = Workbooks.Open("c:\MyBigBook.xlsx")

If wkBook1.ReadOnly Then
    wkBook1.Close False
End If

Note that it is the ReadOnly property that yields the desired info. If you need to check the file ahead of time, you might try using some of the file-access statements available in VBA. That's what the following function does.

Function FileIsLocked(strFileName As String) As Boolean
    FileIsLocked = False
    On Error Resume Next
    Open strFileName For Binary Access Read Write Lock Read Write As #1
    Close #1

    ' If an error occurs, the document is currently open
    If Err.Number <> 0 Then
       FileIsLocked = True
       Err.Clear
    End If
End Function

To use the function, pass it the name (including full path) of the workbook you want to check. The function returns True if the file is locked and False if it isn't. Remember, though, that from the time this function checks the file to the time that you actually try to open the file, it could have been opened by someone else. Thus, the first approach (checking after trying to open) may be the best approach to use.)

It should be noted, as well, that you could also save the other workbooks as shared workbooks. This would allow them to be opened by multiple users with no problems. Of course, you'll want to check how this approach affects the data you may be wanting to save in the workbooks.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (5831) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Editing Custom Dictionaries

Custom dictionaries can be great, but they take quite a bit of time to create. Word provides a way you can edit your ...

Discover More

Embedding Your Phone Number in a Document

One way you can designate your responsibility for a document is to add your phone number to it. There is no need to add ...

Discover More

Normal Words Flagged by Spell Check

What do you do when Word's spelling check marks some common, everyday words as wrong? Here are some ideas of places you ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Determining an ANSI Value in a Macro

Need to know the character code used for a particular character? In a macro you can use the Asc function to determine the ...

Discover More

Opening a Workbook but Disabling Macros

Macros that run automatically when you open or close a workbook are quite helpful. You may not want them to run, however, ...

Discover More

Trimming Spaces from Strings

Need to get rid of extraneous spaces before or after the text in a string? VBA provides three different functions you can ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five less than 5?

2024-03-30 15:54:37

J. Woolley

I suggest modifying the Tip's first VBA code block as follows:

    Set wkBook1 = Workbooks.Open("c:\MyBigBook.xlsx")
    If wkBook1.ReadOnly Then
        wkBook1.ChangeFileAccess Mode:=xlReadWrite, Notify:=True
    End If

If the workbook was previously opened by others, the first message will be displayed (see Figure 1 below) and it will be opened in read-only mode. If the user clicks Notify in the first message, then when all others have closed the workbook the second message will be displayed (see Figure 2 below) and the user can reopen the workbook in read-write mode, with the third message (see Figure 3 below) displaying available options.

Figure 1. 

Figure 2. 

Figure 3. 


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.