ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Quit Excel if ONLY Workbook Open else Close Excel instead (https://www.excelbanter.com/excel-programming/381237-quit-excel-if-only-workbook-open-else-close-excel-instead.html)

Corey

Quit Excel if ONLY Workbook Open else Close Excel instead
 
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....



NickHK

Quit Excel if ONLY Workbook Open else Close Excel instead
 
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

"Corey" wrote in message
...
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

Quit Excel if ONLY Workbook Open else Close Excel instead
 
Thank You Nick.
Perfectly done..


"NickHK" wrote in message
...
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

"Corey" wrote in message
...
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....








All times are GMT +1. The time now is 09:56 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com