Before Close Event Handler

You can perform an action before the workbook is closed and cancel the close if necessary. For example, you might want to validate data before closing, or you might want to ensure that the user wants to close the workbook, as shown in Listing 7.45.

Listing 7.45. Ensuring that the user wants to close the workbook

Private Sub ThisWorkbook_BeforeClose(ByRef Cancel As Boolean) _ Handles Me.BeforeClose

Dim Response As Integer = MsgBox("Are you sure you " _

& "want to close the workbook?", MsgBoxStyle.YesNo, _ "Workbook Close")

If Response = vbNo Then

Cancel = True End If

End Sub

0 0

Post a comment