views:

33

answers:

3

I would like to execute some code when a user closes a form using the x button in the top right corner of the window (I have the form load when the excel spreadsheet is opened, and it hides Excel. I want to exit excel once the form is closed, or at least show excel again so the user may exit it manually)

Looking at the form properties, the Unload property is not present, nor am I able to figure out how to make a function which executes when the form is closed.

Unfortunately, coding this in VB is not an option, it must be VBA.

I'm aware of the code needed to unhide Excel or quit it outright, just not how to tie it to the unload event.

Thanks in advance.

A: 

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

Michael
Thanks, but how does this allow me to execute code when the user clicks the x button? Perhaps I didn't make the question clear, I'll edit it now.
Pixotic
Oh I see, I (incorrectly) assumed the user was exiting via a form button click, not the x button on the window.
Michael
A: 

try something like this

Private Sub Form1_FormClosing(sender as Object, e as FormClosingEventArgs) _ Handles Form1.FormClosing

//Code you want to execute

End Sub

Daniel
Threw up an error, stating _ is an invalid character.
Pixotic
There should be a FormClosing event that you can select for your Form, when you are looking at the source code. Here is a link to a example: http://img385.imageshack.us/img385/3966/wat3am9.jpg
Daniel
A: 

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

Private Sub userform_terminate()

    'Code goes here

End Sub
Pixotic