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


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

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10985) applies to Microsoft Excel 2007, 2010, and 2013. 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. ...


Using Crop Marks with a PostScript Printer

Want to add crop marks to a printout? It's easy to do, provided you are using a PostScript printer.

Discover More

Improving Text Sharpness with ClearType

Ever notice that even with a fancy new monitor, text can still look a bit muddy on the screen. You can use ClearType ...

Discover More

Preserving the Undo List

The undo list can be a lifesaver when working in a macro. Unfortunately, the undo list is not preserved when you run a ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Renaming a Macro

Got a macro that doesn't have quite the right name? You can rename the macro by following these simple steps.

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

Transferring Data between Worksheets Using a Macro

Macros can be used for all sorts of data processing needs. One need that is fairly common is the need to move data from ...

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 five less than 7?

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.