Global variable destroyed when form closed

F

ffyring

I have an Excel application with some forms and some global variables.

For one of the forms I have a problem. If I close the form by pressing
the "x" in the right corner, all my global variables get cleared. If I
close it by using userform.hide then it's no problems.

There is no collision of variable names, and I don't have any macros in
the form so I can't understand what happens to my global variables.

My other forms does not have this problem.

I would very much appreciate any help in this matter!

Best Regards
Fredrik
 
J

Jan Karel Pieterse

Hi,
For one of the forms I have a problem. If I close the form by pressing
the "x" in the right corner, all my global variables get cleared. If I
close it by using userform.hide then it's no problems.

Clicking the X effectively unloads the form from memory. You should
include this event in the form's code:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then
'Hide the form
Me.Hide
'Prevent unloading
Cancel = True
Else
'Do other stuff if some other action closes the form
End If
End Sub

Regards,

Jan Karel Pieterse
Excel MVP
http://www.jkp-ads.com
Member of:
Professional Office Developer Association
www.proofficedev.com
 
N

NickHK

Fredrik,
Sounds like you have declared those variables on the form.
Put them in a module and they will remain until the workbook is closed.

NickHK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top