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

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,214,784
Messages
6,121,540
Members
449,038
Latest member
Guest1337

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