|
|
|
|
|
|
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:
|
- Open Excel.
- Alt + F11 to open the VBA.
- Insert | Module.
- Paste the code there.
|
Test the code:
|
- In another macro add the following code:
- 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.
|
|