Close a workbook, prompt to save, then save workbook and Excel

Fahge

New Member
Joined
Mar 17, 2016
Messages
1
I’ve noticed many people trying to find code that on closing a workbook will prompt to save (if changes were made) and then close the workbook and close Excel. The following code should do the trick.

Place this code in a module and link to a command button or shape
Sub CloseWkBk()
ActiveWorkbook.Close
End Sub
*******************************************
Place the code below in ThisWorkbook
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim wBook As Workbook
Dim LCount As Long
If Cancel = False Then
For Each wBook In Workbooks
If wBook.Name <> Me.Name And UCase(wBook.Name) <> "PERSONAL.XLS" Then
LCount = LCount + 1
End If
Next wBook
If LCount = 0 Then Application.Quit
End If
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

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