Excel

Function To Check If a Workbook Is Open Or Not

Ease of Use

Easy

Version tested with

2002 

Submitted by:

Jacob Hilderbrand

Description:

This User Defined Function can be used to check if a workbook is open or not. 

Discussion:

You have some code you need to run on a specific workbook, but you don't know if the workbook is already open. This function will return a TRUE or FALSE if the workbook is open or not. Then for example, if the value is FALSE you can run additional code to open the workbook. 

Code:

instructions for use

			

Option Explicit Public Function IsWorkBookOpen(WorkBookName As String) Dim WB As Workbook On Error Resume Next Set WB = Workbooks(WorkBookName) On Error GoTo 0 IsWorkBookOpen = Not WB Is Nothing End Function

How to use:

  1. Open Excel.
  2. Alt + F11 to open the VBA.
  3. Insert | Module.
  4. Paste the code there.
 

Test the code:

  1. In another macro add the following code:
  2. IsWorkBookOpen("Book1.xls") '{Where Book1.xls is the name of the workbook you want to test is open}.
 

Sample File:

No Attachment 

Approved by mdmackillop


This entry has been viewed 141 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express