Closing via ThisWorkbook vs Application.Quit

AndyEd

Board Regular
Joined
May 13, 2020
Messages
124
Office Version
  1. 365
Platform
  1. Windows
I am using the following code to Close the Workbook within which the code is being used.

VBA Code:
ThisWorkbook.Close SaveChanges:=True

If another Workbook is open it closes this Workbook and everything appears normal, yet if this is the ONLY Workbook open, it leaves excel live.

VBA Code:
Application.Quit

obviously closes ALL open excel workbooks.

How do you specify fully closing excel if only the one Workbook is open?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
If another Workbook is open it closes this Workbook and everything appears normal, yet if this is the ONLY Workbook open, it leaves excel live.
That is because they are not the same thing! You can have the Excel application open with no workbook open.
So just closing a workbook will NEVER completely close the Excel application, even if it is the only workbook open.

So you would use your first code if you just want to close a single workbook without closing Excel.
You would use your second one if you want to shut down Excel completely (and whatever workbooks are currently open in that session of Excel).

So the second code will close all workbooks and shut-down Excel.
The first code will just close a single workbook.
 
Upvote 0
So is there a way to check to see if other workbooks are open, and if so, leave excel open.

If there are no other workbooks open, shut down excel.

?
 
Upvote 0
So is there a way to check to see if other workbooks are open, and if so, leave excel open.

If there are no other workbooks open, shut down excel.

?
You could check/count the number of open workbooks first, and if it is 1, quit the application, otherwise just close the workbook, i.e.
VBA Code:
Sub MyCloseQuit()
    If Workbooks.Count = 1 Then
        Application.Quit
    Else
        ThisWorkbook.Close SaveChanges:=True
    End If
End Sub
 
Upvote 1
Solution
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top