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.
Written by Allen Wyatt (last updated July 26, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
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:
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.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
Excel allows you to fill a cell's background with just about any color you want. If you need to determine the RGB value ...
Discover MoreDo you want a way to reverse names within a cell, making them "last, first" instead of "first last?" Here's a handy macro ...
Discover MorePerhaps the most common way of communicating with programs is through the use of dialog boxes. We expect dialog boxes to ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
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
@ 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
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments