Initializing a UserForm
-
- 5StarLounger
- Posts: 689
- Joined: 27 Jan 2010, 16:45
- Location: Ottawa, Ontario, Canada
Initializing a UserForm
Running Excel 2003 SP3
Just now a userform was displayed with unexpected items listed in a list box. It occurs to me that I don't understand what triggers a userform initialize procedure.
Can someone enlighten me? Please!
TIA
Just now a userform was displayed with unexpected items listed in a list box. It occurs to me that I don't understand what triggers a userform initialize procedure.
Can someone enlighten me? Please!
TIA
Regards
Don
Don
-
- Administrator
- Posts: 78631
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Initializing a UserForm
A userform is initialized when it is loaded into memory. You can load it explicitly:
Load UserForm1
and if you show a userform that hasn't been loaded yet, loading it is implicit:
UserForm1.Show
There are two ways you can dismiss a userform: hiding and unloading.
Me.Hide
or
UserForm1.Hide
will make the form invisible while it remains in memory. All controls retain their values. If you next show the userform again:
UserForm1.Show
it will NOT be initialized because it is still loaded in memory. So it'll display the "old" values unless you take care to remove them.
Unload Me
or
Unload UserForm1
will close the form and remove it from memory. So next time you show the form, it will be initialized again.
Load UserForm1
and if you show a userform that hasn't been loaded yet, loading it is implicit:
UserForm1.Show
There are two ways you can dismiss a userform: hiding and unloading.
Me.Hide
or
UserForm1.Hide
will make the form invisible while it remains in memory. All controls retain their values. If you next show the userform again:
UserForm1.Show
it will NOT be initialized because it is still loaded in memory. So it'll display the "old" values unless you take care to remove them.
Unload Me
or
Unload UserForm1
will close the form and remove it from memory. So next time you show the form, it will be initialized again.
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 689
- Joined: 27 Jan 2010, 16:45
- Location: Ottawa, Ontario, Canada
-
- Microsoft MVP
- Posts: 656
- Joined: 24 Jan 2010, 17:51
- Status: Microsoft MVP
- Location: Weert, The Netherlands
Re: Initializing a UserForm
What I usually do is add a public sub called Init to the form, which I can call whenever I see fit. That way I always know when the form will initialize.
When I want to show the form I then do this (air code):
When I want to show the form I then do this (air code):
Code: Select all
Dim ufForm as UserForm1
Set ufForm = New UserForm1
With ufForm
.Init
.Show
End With
-
- 5StarLounger
- Posts: 689
- Joined: 27 Jan 2010, 16:45
- Location: Ottawa, Ontario, Canada
-
- 5StarLounger
- Posts: 689
- Joined: 27 Jan 2010, 16:45
- Location: Ottawa, Ontario, Canada
Re: Initializing a UserForm
I am about to institute the approach suggested by Jan Karel; primarily as a consequence of an oddity which I have come across. I found that the following code was not initializing my userform.
So I added line 25;With line 25, there was no indication that the form was being initialized either when running or when stepping through the code. The next step was to add line 22 to force an initialization at line 25To my amazement line 22 caused the code to branch to frmSelTxt.UserForm_Initialize before getting to the Load command.
Do I have an oddity/corruption in my Excel 2003 installation?
Can other loungers duplicate my observations?
Code: Select all
Public Sub Activate_FG_Sht()
10 If FG_Defn_Sht = "New Item Class Req'd." Then
20 SelTxtCaller = 1
30 frmSelTxt.Show
40 End If
End Sub
Code: Select all
25 Load frmSelTxt
Code: Select all
22 Unload frmSelTxt
Do I have an oddity/corruption in my Excel 2003 installation?
Can other loungers duplicate my observations?
Last edited by Don Wells on 22 Feb 2012, 13:29, edited 1 time in total.
Regards
Don
Don
-
- Administrator
- Posts: 12629
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe
Re: Initializing a UserForm
If the form has been previously loaded, and not unloaded again, then this is the expected behavior.
StuartR
-
- Administrator
- Posts: 78631
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Initializing a UserForm
I too can confirm that this happens, but does it matter?
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 689
- Joined: 27 Jan 2010, 16:45
- Location: Ottawa, Ontario, Canada
Thank you Stuart and Hans
I believe that it does matter. If I had understood this earlier, several hours would have been spent more usefully.StuartR wrote:If the form has been previously loaded, and not unloaded again, then this is the expected behavior.
Regards
Don
Don
-
- 5StarLounger
- Posts: 689
- Joined: 27 Jan 2010, 16:45
- Location: Ottawa, Ontario, Canada
Re: Initializing a UserForm
The following code was provided by Jan Karel earlier in this thread.
Can someone advise me on the benefit of the term 'New' in the 2nd line?
T.I.A.
Code: Select all
Dim ufForm as UserForm1
Set ufForm = New UserForm1
With ufForm
.Init
.Show
End With
T.I.A.
Regards
Don
Don
-
- Administrator
- Posts: 78631
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Initializing a UserForm
The line
creates a new instance of UserForm1. So regardless of whether an instance of UserForm1 is still in memory (because it has only been hidden, not unloaded) or not, the UserForm_Initialize code will run for ufForm.
Code: Select all
Set ufForm = New UserForm1
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 689
- Joined: 27 Jan 2010, 16:45
- Location: Ottawa, Ontario, Canada