Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 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

Written by Allen Wyatt (last updated July 26, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


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

Ignoring Smart Quotes when Comparing Text

When comparing two pieces of text, you may find that Word's smart quotes can mess up the comparison. Here's a quick way ...

Discover More

ExcelTips Ribbon 2020 Archive (Table of Contents)

ExcelTips is a weekly newsletter that provides tips on how to effectively use Microsoft's best-selling spreadsheet ...

Discover More

Determining an Integer Value

One of the math functions you can use in your macros is the Int function. It provides a way for you to derive an integer ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

DOS from Macros

Need to run a DOS command from within one of your macros? The answer is the Shell command, described in this tip.

Discover More

Making Worksheet Copies for Daily Shifts

Creating lot of copies of a worksheet is a snap within a macro. This tip introduces a macro that will make three copies ...

Discover More

Removing All Macros

Macros are stored as part of a workbook so that they are always available when you have the workbook open. If you want 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}] (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 8 + 7?

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.