7

How can I call UserForm_Initialize() in a Module instead of the UserForm code object?

3 Answers 3

14

From a module:

UserFormName.UserForm_Initialize

Just make sure that in your userform, you update the sub like so:

Public Sub UserForm_Initialize() so it can be called from outside the form.

Alternately, if the Userform hasn't been loaded:

UserFormName.Show will end up calling UserForm_Initialize because it loads the form.

Sign up to request clarification or add additional context in comments.

8 Comments

Hi, so where should I put the UserFormName.UserForm_Initialize?
Wherever you like... any vba in the workbook within a Sub or Function.
Thanks for the method Daniel. Can I put Me to let it Initialize itself? e.g I got a lot of UserForm(s) and I do not want to code each of them.
you can use a for each uf in UserForms loop?
In the function, I tried to declare the UserFormName As UserForm/String and then called the name of the form in the parameter but still no luck with that :(
|
3

IMHO the method UserForm_Initialize should remain private bacause it is event handler for Initialize event of the UserForm.

This event handler is called when new instance of the UserForm is created. In this even handler u can initialize the private members of UserForm1 class.

Example:

Standard module code:

Option Explicit

Public Sub Main()
  Dim myUserForm As UserForm1

  Set myUserForm = New UserForm1
  myUserForm.Show

End Sub

User form code:

Option Explicit

Private m_initializationDate As Date

Private Sub UserForm_Initialize()
  m_initializationDate = VBA.DateTime.Date
  MsgBox "Hi from UserForm_Initialize event handler.", vbInformation
End Sub

8 Comments

Hi Daniel, good thought there. What I wanted is e.g. create a function for UserForm_Initialize in the Module (which I will not specify which UserForm to init YET). Regarding which UserForm to init, I will later include it in the UserForm. The reason I did this is because I wanted to include the init in the IF ELSE statement in the Module.
I do not understand the meaning of the comment u wrote, sorry. In general u should avoid calling UserForm_Initilize() directry because it will be called by the class UserForm1 itself as a reaction to creation of UserForm1. It is symply an event-handler so it should handle event.
1. I have IF ELSE statement in Module1 2. Before END IF, I want to reset UserForm1 3. I want the 'reset' function inside the Module1 P.S Sorry for my bad explanation
It's OK Tan, my english is bad as well :-). Couldn't u just post your code sample?
I've resolved my problem and posted the solution in my own question above. Do you think it is good?
|
1

SOLUTION After all this time, I managed to resolve the problem.

In Module: UserForms(Name).Userform_Initialize

This method works best to dynamically init the current UserForm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.