Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Checking if a Workbook is Already Open.

Checking if a Workbook is Already Open

by Allen Wyatt
(last updated January 5, 2019)

5

Macros are often used to slice, dice, and otherwise process information contained in workbooks. This presumes, of course, that the workbook that contains the information is actually open. If it is not, then your macro will obviously need to include code to actually open the needed workbook.

Opening a workbook can really slow down a macro; it takes time to access the disk and load the file. Thus, if your macro can check to see if a workbook is open before going through the hassle of actually trying to open it, you could speed up your macros greatly if the workbook is found to already be open.

One very flexible way to approach the task of checking whether a workbook is open is to use a function that does the checking, and then simply returns a TRUE or FALSE value based on whether the workbook is open. The following short macro performs this succinct task:

Function AlreadyOpen(sFname As String) As Boolean
    Dim wkb As Workbook
    On Error Resume Next
    Set wkb = Workbooks(sFname)
    AlreadyOpen = Not wkb Is Nothing
    Set wkb = Nothing
End Function

To use the function, just pass it the name of the workbook you want to check, in the following manner:

    sFilename = "MyFileName.xls"
    sPath = "C:\MyFolder\MySubFolder\"
    If AlreadyOpen(sFilename) Then
        'Do not have to open
    Else
        Workbooks.Open sPath & sFilename
    End If

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 (10985) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Checking if a Workbook is Already Open.

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

Setting a Default for the Object Browser

Does it bother you that when you press Ctrl+Page Up or Ctrl+Page Down you aren't always taken to the top of the previous ...

Discover More

Different Table Captions on Multiple Pages

If you have a table that spans multiple pages, you may want to add a caption to the table and have that caption use ...

Discover More

Determining Month Names for a Range of Dates

Given a starting date and an ending date, you may want to generate the names of all the months between those two dates. ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Workbook Events

You can create macros that run whenever Excel detects a certain event happening within an entire workbook. This tip ...

Discover More

Continuing Macro Lines

Sometimes a macro command line can get very, very long. This can make it hard to understand when you look at it a month ...

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 ...

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 eight less than 8?

2019-01-13 09:44:50

Simon Freeman

Alans - many thanks. - Simon


2019-01-07 12:44:42

Alan F. Bouvier

Our system has shared networks mapped to drive letters. I have seen the file paths show up with either the drive letter or the full server path.
Since Excel includes the path as part of the file name, sometimes is doesn't recognize that a file is open.
I usually just strip off the path and compare the file name; however, a more accurate method would be to force the paths to be canonical and then compare.
This then leaves you open to the possible error of "Excel can't open two files with the same name."


2019-01-07 05:10:46

Alan Elston

P.S.
Set wkb = Workbooks(sFname) will not work if workbook sFname is closed because
Workbooks
is a "collection" object of all open workbooks. So workbook sFname will not be found in that collection object, so that code line will error. ( Our running routine will not error as On Error Resume Next tells VBA to ignor an error and just keep going after the line that errored )


2019-01-07 05:05:59

Alan Elston

@ Simon Freeman
Re AlreadyOpen = Not wkb Is Nothing

VBA will usually read things from approximately right to left , ( unless you put things in brackets, as in normal maths, to ensure that some things are done in a specific order )
So this is what VBA does in the following order

wkb Is Nothing
This will return True if wkb is Nothing and False if wkb is something.
(wkb will be something if this bit did not error: Set wkb = Workbooks(sFname). That is to say wkb will be something if the workbook named sFname is open, because then Set wkb = Workbooks(sFname) will have worked to fill the variable wkb. If that command had not worked, then wkb will be at its initial default state of Nothing

Not wkb Is Nothing
This Not simply puts a False to True and a True to a False

So example.
Say your Workbook, sFname is closed.
Set wkb = Workbooks(sFname) will not work so wkb stays at its default initial state of Nothing
wkb Is Nothing will give True, because it is true that wkb Is Nothing
Not wkb Is Nothing will change that True to False
So then AlreadyOpen = Not wkb Is Nothing
…. , AlreadyOpen = False …….which is the result you want

Alan Elston


2019-01-06 05:38:40

Simon Freeman

Alan - could you explain this line from the first Macro: AlreadyOpen = Not wkb Is Nothing - Thanks - Simon


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.