Unload or Hide a UserForm

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Unload or Hide a UserForm

Post by ABabeNChrist »

I was just wondering when using Unload or Hide of a UserForm, when is it best to one or the other. Let say if I go in and out of a UserForm1 frequently, would I just Hide, but if I were to selected a UserForm2 that is rarely used, would I then use Unload. Or does it even matter.

User avatar
HansV
Administrator
Posts: 78512
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Unload or Hide a UserForm

Post by HansV »

It depends on how you use the form.

If you hide the form, it stays in memory, and all controls retain their value, as well as VBA variables. So when you show the form again, it will look exactly as when you hid it. The UserForm_Initialize event that ran when you first opened the form, will not run again when you unhide it.

If you unload a form, it is removed from memory. So when you open it again, all controls and variables have been cleared, and the UserForm_Initialize event will occur.
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Unload or Hide a UserForm

Post by ABabeNChrist »

Thank You Hans

User avatar
StuartR
Administrator
Posts: 12611
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Unload or Hide a UserForm

Post by StuartR »

Also if you just hide a form then when you unhide it again it tends to change focus to the document that was active when you hid the form. I am not sure if this is the exact behaviour, but I do know that it is best to unload and reload the form if you care about which document has focus and you don't want to retain status in the form.
StuartR