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

Starting Microsoft Graph

Microsoft Graph is a simplistic graphic tool that you can use to quickly add graphs to your document. Here's how to start ...

Discover More

Offering Options in a Macro

When creating macros, you often need to offer a series of choices to a user. This tip demonstrates how easy it is to ...

Discover More

Searching for Non-Black Text

Searching for text having (or not having) specific formatting is generally pretty easy. It is more difficult to search ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Converting Strings to Numbers

When working with data in a macro, there are two broad categories you can manipulate: numbers and text. Sometimes you ...

Discover More

Creating a Function Inventory for a Workbook

Your worksheets are very often made up of formulas and these formulas are made up of functions. If you ever want to ...

Discover More

Understanding the While...Wend Structure

Logical structures are important in programming, as they allow you to control how the programming statements are ...

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 three less than 5?

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.