Theoretical question

alcorjr

Active Member
Joined
Dec 29, 2002
Messages
416
Hi guys, can anybody explain to me or provide a link to the effect, on what is the difference between a Userform Activate and Userform Initialize event ?
Thanks a lot :biggrin:
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

tusharm

MrExcel MVP
Joined
May 28, 2002
Messages
11,029
What does help contain on the subject? Where do you get stuck / confused?
 

Mudface

MrExcel MVP
Joined
Feb 18, 2002
Messages
3,339
The Initialize event fires first and occurs as soon as you reference the form's name in your code before the form becomes visible.

The Activate event fires when the form first becomes visible or when you shift focus from another object.
 

Monkey

Active Member
Joined
Feb 10, 2003
Messages
251
This is the way I see it,

With Userform activate the procedure runs when it is Selected or on an other way activated.
The userform Initialize just loads conditions you've set on the userform. In this case there is no interaction between human and machine.

I could be mistaking like I said earlier. If I am please let us all know


Greetings,

monkey
 

alcorjr

Active Member
Joined
Dec 29, 2002
Messages
416

ADVERTISEMENT

Thanks everybody for your quick reply.
Well, the vba help is quite cryptic, as is often the case.
I'm not getting stuck anywhere, the thing is that as you mention, both initialize and activate events run whenever a control or object belonging to that userform is mentioned in other parts of the code.
Now, I have some script that resets certain variables when that userform is "opened". I only want them to be reset when it is "opened", not every time it's mentioned. Currently I'm preventing this to happen with the use of some flags, but I was wondering if I had included the "reset" script under the right event. I had it under "ACTIVATE" and then I changed it to "INITIALIZE", but I'm not getting the desired effects (I still have to use the flags).

Thanks a lot from sunny Costa Rica :p :p
 

tusharm

MrExcel MVP
Joined
May 28, 2002
Messages
11,029
alcorjr said:
Well, the vba help is quite cryptic, as is often the case.
Obviously, we disagree ;)
alcorjr said:
both initialize and activate events run whenever a control or object belonging to that userform is mentioned in other parts of the code.
No, no one wrote that and neither does help indicate that. Try this test with XL2000 or later. It will make very explicit how the events work.
Create two userforms named Userform1 and Userform2. In the code module for Userform1, put in:

Code:
Option Explicit

Private Sub UserForm_Activate()
    MsgBox "In Userform1_Activate"
    End Sub

Private Sub UserForm_Click()

End Sub

Private Sub UserForm_Initialize()
    MsgBox "In Userform1_Initialize"
    End Sub

For Userform2, use:

Code:
Option Explicit

Private Sub UserForm_Activate()
    MsgBox "In Userform2_Activate"
    End Sub

Private Sub UserForm_Click()

End Sub

Private Sub UserForm_Initialize()
    MsgBox "In Userform2_Initialize"
    End Sub

Now, in a standard module, enter
Code:
Sub testIt()
    Load UserForm1
    Load UserForm2
    UserForm1.Show vbModeless
    Application.Wait Now() + TimeValue("0:0:3")
    UserForm2.Show
    End Sub

Run the code in testIt. You will get the messages:
Userform1_Initialize
Userform2_Initialize
Userform1_Activate
Userform2_Activate


Close the open Userform2 and you will get
Userform1_Activate

Close Userform1 and the test will end.

alcorjr said:
Thanks a lot from sunny Costa Rica :p :p
Go ahead and make us feel bad. {g} Sub-normal temperatures and super-normal precipitation for a record number of days!
 

alcorjr

Active Member
Joined
Dec 29, 2002
Messages
416
Tushar, thanks. Your reply was quite a mouthful. It took me this time to digest it.
So, I think this is what I learnt ;

the load command will trigger the Initialize event

The show command or any click on the userform itself will trigger the Activate event, and so will any reference to an object on the userform,
eg. ; na=Userform1.textbox1.value

Right??

:LOL: 19 degrees C. min - 25 Celsius max. no rain til September
 

tusharm

MrExcel MVP
Joined
May 28, 2002
Messages
11,029
alcorjr said:
Tushar, thanks.
You are welcome.
alcorjr said:
The show command or any click on the userform itself will trigger the Activate event, and so will any reference to an object on the userform,
eg. ; na=Userform1.textbox1.value

Right??
No. The Activate event triggers when the object is activated, i.e., becomes the active window. It is exactly as defined in the help file. Experiment for yourself using a testbed analogous to what I posted earlier.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,151,871
Messages
5,766,857
Members
425,382
Latest member
IronM

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top