Quit Excel if ONLY Workbook Open else Close Excel instead

  • Thread starter Thread starter Corey
  • Start date Start date
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....
 
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
 
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
 
Back
Top