Quit Excel if ONLY Workbook Open else Close Excel instead

C

Corey

I currently use:

Sub Button14_Click()
Application.DisplayAlerts = Fasle
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.DisplayAlerts = True
End Sub

Is there a way for Excel to Check if ANY other Workbooks are Open or Not.
If NO others are Opened, then to QUIT rather than to CLOSE ???

But if there is another Workbook open then Excel simply Closes this Workbook
an NOT Quitting Excel.


Can you do this?

Corey....
 
N

NickHK

Corey,
You can check the Workbooks collection, allowing for the WB that contains
this code and the possibility of hidden workbooks, notably Personal.xls.
something based on:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim WB As Workbook

For Each WB In Application.Workbooks
Debug.Print WB.Name
If WB.Name <> ThisWorkbook.Name Then
If WB.Windows(1).Visible = True Then Exit Sub
End If
Next

Application.Quit

End Sub

NickHK
 
C

Corey

Thank You Nick.
Perfectly done..


NickHK said:
Corey,
You can check the Workbooks collection, allowing for the WB that contains
this code and the possibility of hidden workbooks, notably Personal.xls.
something based on:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim WB As Workbook

For Each WB In Application.Workbooks
Debug.Print WB.Name
If WB.Name <> ThisWorkbook.Name Then
If WB.Windows(1).Visible = True Then Exit Sub
End If
Next

Application.Quit

End Sub

NickHK
 

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