FORMS - Initialize vs Activate Whats the difference?

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,347
Office Version
  1. 365
Platform
  1. Windows
FORMS - Initiate vs Activate Whats the difference?

If I am putting code into a userform (to formate some texboxes) whats the difference between Initialize and Activate???
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Use Initialize, when the Form is commited to Memory. Activate simply means it's the Active Window, avoid the latter.
 
Upvote 0
Hi, gheyman,

could this example show it ?
you need a userform with a commandbutton (default name accepted)
Code:
Option Explicit

Private Sub CommandButton1_Click()
Unload Me
End Sub

Private Sub UserForm_Activate()
MsgBox "start", 64, "activate"
userformcode
MsgBox "middle", 64, "activate"
userformcode
MsgBox "end", 64, "activate"
End Sub

Private Sub UserForm_Initialize()
MsgBox "start", 64, "initialize"
userformcode
MsgBox "middle", 64, "initialize"
userformcode
MsgBox "end", 64, "initialize"
End Sub

Private Sub userformcode()
Dim i, j
    With CommandButton1
        For i = 1 To 50
            For j = 1 To 50
            .Left = Application.Max(24, .Left + IIf((i Mod 2) = 0, 1, -1) * 2)
            Me.Repaint
            Next j
        Next i
    End With
End Sub
kind regards,
Erik

EDIT: Hi, Nate :-)
as so often didn't see those replies
 
Upvote 0
...Activate simply means it's the Active Window...
-NateO

Heya Nate,

Hope this doesn't come across as argumentative, but I'm not quite sure I'd would phrase it quite that way. I agree that Initialize happens when the form is commited to memory - typically using LOAD or SET ufXXX = USERFORM1. But I'd be more inclined to say that Activate is what gets called when you Show the userform. My point is that if you show a form using vbModeless and you switch focus from one window to another the userform's Activate method does not get called each time focus returns to the UserForm - a behavior that is different than WS or WB Activate methods.

Kind regards,
 
Upvote 0
True, I was thinking it might...

Still might go with Initialize, just from a Language/Event standard point of view... :)
 
Upvote 0
As has already been pointed out, the Initialize event occurs when a userform is first loaded into memory. In most cases that happens when the programmer does a UF1.Show but it can also be done with a Load UF1. The latter has the advantage of instantiating the userform object w/o showing it to the user.

The Activate event occurs when the userform is shown. So, code like
Code:
load uf1
application.wait now()+timeserial(0,0,3)
uf1.show
would first trigger the initialize event and after a pause of 3 seconds trigger the activate event and then show the userform.
When does one use which event? I use the Initialize event to do things that I want to happen when the userform is loaded in memory but not repeated each time it is shown.

The code in the activate event procedure is something I want executed each time the userform is shown.

What's the difference? It follows from how I handle the user dismissing the userform. Almost always I just hide it. Very, very rarely will I unload it. The reason is that the next time I show the form -- if there is a next time -- all the variables used for behind-the-scenes processing are already available and all the values the user entered in the different controls the last time around are shown automagically.

So, if there is any "on the fly" customization I have to do to the userform (for example, add a timestamp or additional controls or resize the UF), it almost always goes in the Activate procedure. The Initialize procedure is used to set up behind-the-scenes data structures that will persist across the userform being shown and hidden.

FORMS - Initiate vs Activate Whats the difference?

If I am putting code into a userform (to formate some texboxes) whats the difference between Initialize and Activate???
 
Upvote 0
Thanks for expounding [just a wee bit] upon my rather terse "kinda depends on what you're up to". :-D

Oh and
...automagically...
-Tushar
Intentional pun or just got lucky? :razz:
 
Upvote 0
tusharm Thank you

I came here just to find out what the difference was between Initiate and Activate. I have self-taught myself how to write VBA code through the knowledge of other programming I have done. You have just changed the way I will use Userforms forever. Thank you for your description above on how to load and show user forms and how to initiate and activate are utilized with them. A perfect answer to the question
 
Upvote 0

Forum statistics

Threads
1,224,804
Messages
6,181,060
Members
453,017
Latest member
rlundbulls23

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
Back
Top