Check if workbook is already open, if so close

M

Mischa Browne

Hi,

Trying to write a macro to close and check if a workbook is already open and
open sub Show afterwards.

If the workbook is ALREADY open, then it must be closed first - and the
workbook must be re-opened - and the Sub Show must run.
_________
If the workbook is NOT open, then it must be opened - and the Sub
Show must run.


If have tried to do it like below, of course it does not work.
Could one of you Guru's please help me?

*******************************************
Sub Check

If Not ("Book1").Active Then _
Workbooks("Book1").Close SaveChanges:=False
Application.Run ("Book2.xls!Show")

Else Application.Run ("Book2.xls!Show")
End If

End sub
*******************************************
 
P

papou

Hello Mischa
On Error Resume Next
Windows("Book1").Activate
If Err = 0 Then Workbooks("Book1").Close (False)

HTH
Cordially
Pascal
 
N

NooK

Here is the code to check whether a workbook is open or not, I think yo
might be able to develop from there

'Checks if a Workbook is open
Function WorkbookOpen(WorkBookName As String) As Boolean
' returns TRUE if the workbook is open
WorkbookOpen = False
On Error GoTo WorkBookNotOpen
If Len(Application.Workbooks(WorkBookName).name) > 0 Then
WorkbookOpen = True
Exit Function
End If
WorkBookNotOpen:
End Function

Best Regards

Noo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top