Calling UserForm_Initialize() in a Module

By | February 27, 2018
Questions:

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

Answers:

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.

Questions:
Answers:

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

Questions:
Answers:

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

Leave a Reply

Your email address will not be published. Required fields are marked *