Testing if a Workbook is Open

by Allen Wyatt
(last updated February 8, 2016)

4

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.

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

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

Changing an AutoShape

Need to change a shape you previously added to your worksheet? It's easy to do using the graphics tools provided by Excel. ...

Discover More

Refreshing PivotTable Data

If you modify the data on which a PivotTable is based, you'll need to refresh the table so it reflects the modified data. ...

Discover More

Printing a Chart

It is inevitable that if you spend time creating a chart you will want to print that chart on your printer. Here's how you ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Macros Run Fine Individually, but Not Collectively

Developing macros can be rewarding, but it can also be challenging. Getting individual macros to run properly is hard enough, ...

Discover More

Making Modal Dialog Boxes Appear in Front of Workbooks

Perhaps the most common way of communicating with programs is through the use of dialog boxes. We expect dialog boxes to be ...

Discover More

Deleting Zero Values from a Data Table

Want to get rid of all the zero values in a range of cells? This tip provides a couple of different ways you can accomplish ...

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}] 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 four less than 9?

2014-06-16 14:14:22

Donald Parish

This code didn't work for me with files using mapped drives in the name. I used this URL to convert the mapped drive to the UNC path, and that works.
http://social.msdn.microsoft.com/Forums/office/en-US/42c2e942-fcfb-4ff2-9519-d2d7df987ff2/vba-solution-to-insert-unc-path-into-excel-footer?forum=exceldev


2013-07-31 10:05:46

Excel-nt

Every day we learn something new and today I've learned a great deal about functions... disregard my previous plea for help :)

Brian


2013-07-31 09:25:31

Excel-nt

In using the Function option, I'm embarrassed to admit that I don't know how to use it, to be more precise, I don't know how to pass the file name I want to check.

Please help,
Brian


2013-07-31 08:54:41

Excel-nt

This works great, I prefer to use the 'Set' statement but I'm getting 2 different results, in one case I'm getting the "File In Use" notification along with the options of "Read Only, Notify or Cancel" and with the other I don't get this but I do get the "File Now available" when I close it. Is there a way to avoid these notifications entirely?

Thanks,
Brian


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.