Execute code when form is closed in VBA (Excel 2007) Execute code when form is closed in VBA (Excel 2007) vba vba

Execute code when form is closed in VBA (Excel 2007)


You can use QueryClose event of the UserForm as follows:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)    If CloseMode = 0 Then        ' Your codes        ' Tip: If you want to prevent closing UserForm by Close (×) button in the right-top corner of the UserForm, just uncomment the following line:        ' Cancel = True    End IfEnd Sub

You can also use vbFormControlMenu like this:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)    If CloseMode = vbFormControlMenu Then        'Your code goes here    End IfEnd Sub


A colleague was able to provide the answer, including example here for everybody else

Private Sub userform_terminate()    'Code goes hereEnd Sub


You can use Unload Me in VBA to close a form. Just put the code to close Excel immediately following that.