Need a test to see if a workbook is already open

Wild Bill

Board Regular
Joined
Feb 20, 2006
Messages
125
I have a workbook_open macro that opens another workbook. It works great except when the second workbook is already open. I'm looking for a way to test for the other workbook being already open and if it is to prevent an attempt to open it again. Here's the existing macro:

Code:
Private Sub Workbook_Open()

Dim wbk As String

    wbk = ActiveWorkbook.Name
    ActiveWindow.WindowState = xlNormal

    Workbooks.Open Filename:= _
        "\\ST Budget May-06\Reference Data\STaR_Droplists.xls", ReadOnly:=True
    Workbooks("STaR_Droplists.xls").Activate
    ActiveWindow.Visible = False
    
    Workbooks(wbk).Activate
    ActiveWindow.Visible = True
    ActiveWindow.WindowState = xlNormal

End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Here's some code to test if a wb is open:

<font face=Tahoma><SPAN style="color:#00007F">Sub</SPAN> IsWorkBookOpen()
<SPAN style="color:#007F00">''''''''''''''''''''''''''''''''''''''''''</SPAN>
<SPAN style="color:#007F00">'Written by www.ozgrid.com</SPAN>

<SPAN style="color:#007F00">'Test to see if a Workbook is open.</SPAN>
<SPAN style="color:#007F00">''''''''''''''''''''''''''''''''''''''''''</SPAN>

<SPAN style="color:#00007F">Dim</SPAN> wBook <SPAN style="color:#00007F">As</SPAN> Workbook

    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> wBook = Workbooks("Personal.xls")

        <SPAN style="color:#00007F">If</SPAN> wBook <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#007F00">'Not open</SPAN>
            MsgBox "Workbook is not open", _
            vbCritical, "OzGrid.com"
            <SPAN style="color:#00007F">Set</SPAN> wBook = <SPAN style="color:#00007F">Nothing</SPAN>
            <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0
        <SPAN style="color:#00007F">Else</SPAN> <SPAN style="color:#007F00">'It is open</SPAN>
            MsgBox "Yes it is open", _
            vbInformation, "OzGrid.com"
            <SPAN style="color:#00007F">Set</SPAN> wBook = <SPAN style="color:#00007F">Nothing</SPAN>
            <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

</FONT>

HTH,

Smitty
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,236
Members
448,555
Latest member
RobertJones1986

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top