Check if workbook is already open, if so close

  • Thread starter Thread starter Mischa Browne
  • Start date Start date
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
*******************************************
 
Hello Mischa
On Error Resume Next
Windows("Book1").Activate
If Err = 0 Then Workbooks("Book1").Close (False)

HTH
Cordially
Pascal
 
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
 
Back
Top