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 July 2, 2018)


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
        Workbooks.Open sPath & sFilename
    End If


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


Adding Diagonal Borders

Want to add a border diagonally, through the middle of a table cell? It's easy if you follow the formatting steps ...

Discover More

Defeating Automatic Date Parsing

Excel is continually trying to figure out what type of data is being stored in a cell. If it can interpret a value as a ...

Discover More

Adding Video to a Slide

Videos can be an important element of any presentation. Google Presentation makes it easy to add YouTube videos to any ...

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)

Exiting a For ... Next Loop Early

If you use For ... Next loops in your macros, make sure you give a way to jump out of the loop early. That way you can ...

Discover More

Quickly Dumping Array Contents

Variable arrays are used quite often in macros. If you use an array once in your macro and then need to reuse it for ...

Discover More

Determining the Hour of the Day

Need to know the current hour of the day? You can derive the information in your macros by using the Hour function, as ...

Discover More

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

View most recent newsletter.


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 3 + 4?

2016-12-19 14:38:24

Harry S

' Some prefer the approach below for any item
' that Excel has a collection for
' It is that collection that excel uses IN ALL CASES here

'No problems with Errors
'No jumping out of loops with exit sub or exit function
' Not sure if it was .xls .xlsm etc
' exits when found
'Not any better than either of approaches discussed

Function AlreadyOpen(sFname As String) As Boolean
Dim Li%
While Not AlreadyOpen And Li < Workbooks.Count
Li = Li + 1
AlreadyOpen = Workbooks.Item(Li).Name Like sFname & ".xl*"
End Function

2013-09-05 07:52:35

Barry Fitzpatrick

A further consideration is whether or not the file is open in another instance of Excel either on the hosts computer or on another Users computer. In which case the file might have been opened in "Read Only" mode.

The implications of this will depend on what is being done with the workbooks concerned, but will have ramifications if the file is changed in any way and needs to be saved under the same name.

2013-09-04 13:20:57


It's also a best practice to avoid loops when at all possible. Allen's version only ever goes through the code once; yours loops once for every open workbook. Your's is no more "logical" than Allen's, it is just the easiest to come up with because loops are a basic programming topic. Yours may be "simple" (honestly, I find Allen's simpler, even though it's a more advanced programming tactic), but in my mind "simple" doesn't nessecarily correlate with "best".

The actual time difference is neglegible, but my point was not that you would save 0.0005 seconds with one way vs another way; my point is that there's no reason *not* to write good code. If, for some reason, you do one day have 1,000 workbooks open and the one you want is #2 (or for that matter, #999), you will wish you had written proper code. If that day never comes, you've hardly wasted any time, as it is also a neglegible amount of time to add the Exit For in the first place.

Lastly, you misunderstand my point about file location. It's obviously true that you can't have two files open with the same name (although if you open files from the web you *could* have the same name, but with a different number appended to it), but if the purpose is to determine if a *specific* file is open, you need to make sure it's the right one. For instance, let's say your macro is supposed to open C:Folder1Book1.xls and you use one of the above macros to check if "Book1.xls" is open before running the rest of the code. If you had C:Folder2Book1.xls open, your macro would check for "Book1.xls", and, upon finding it, continue on to make changes, even though it's making changes to the wrong book.

One approach (which yours and Allen's code takes) is to simply assume that if you ever have a Book1.xls open it's the *right* Book1.xls. Much like your lack of an Exit For, this will work a good amount of the time, and may very well never lead you astray. However, it's risky programming practice and when it does go wrong you'll be sorry.

2013-09-03 12:06:32

Willy Vanhaelen

I agree with you that there is nothing wrong with using an error but in my opinion it should only be used if there is no alternative and my solution is so simple and evident that I by far prefer that.

And you are right, it is good practice to add an "Exit For" in this case but unless a very large number of files are open, there will be no noticeable gain of execution time.

Excel doesn't allow you to open two files with the same name but in different directories. So this problem cannot occur.

2013-09-02 09:26:35


I don't usually come to Allen's defense on these things, but Willy, your code has its problems too. Using an error like Allen did is actually a pretty common method, and there's nothing wrong with it (though IIRC, if you are using error trapping in your whole project, you need to add "On Error GoTo 0" at the end, or your error trapping won't stop at the correct level). If you go with your route, you need to add an "Exit For" so that you don't keep looping after you've found the file. Your second code snippit is definitely better, however; it doesn't make sense to have an empty code block, so it's better to combine the lines.

The thing that neither of these versions address is the possibility of having a file with the same name that's not the correct file (because it's in a different directory).

2013-08-31 09:58:58

Willy Vanhaelen

It's a strange approach to rely on an error that will occur if you try to set a reference to a workbook that is not open.

Why not use the collection of open workbooks that Excl maintains? This simple macro does that and cycles through the open workbooks and sets the result to true if found.

Function AlreadyOpen(sFname As String) As Boolean
Dim wkb As Workbook
For Each wkb In Workbooks
If wkb.Name = sFname Then AlreadyOpen = True
Next wkb
End Function

The second part can also be simplified:

sFilename = "MyFileName.xls"
sPath = "C:MyFolderMySubFolder"
If Not AlreadyOpen(sFilename) Then Workbooks.Open sPath & sFilename

Seems to be more logical :-)

Willy Vanhaelen

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

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.