Vba Workbook Before Close Cancel. Parameters Auto Run Macro When Workbook Closes. Insert your code between Private Sub and End Sub.
See the code in this picture below. When Excel detects through the UI that the workbook has changed it will ask if the user wants to save the changes. In this tutorial, you will learn how to open and close a Workbook in several ways.
In the below example, when you close the workbook, a message box will prompt to confirm quitting Workbook or not.
The sub is located in the ThisWorkbook tab and was initiated using the workbook --> BeforeClose drop down.
The msgbox displays, but regardless of the option the user chooses, the workbook still close as if the code was not there. Closing a workbook from Visual Basic doesn't run any Auto_Close macros in the workbook. The crux of the code is simple — it asks the users whether they want to close the workbook.