vba 在模块中调用 UserForm_Initialize()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13964780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:38:07  来源:igfitidea点击:

Calling UserForm_Initialize() in a Module

excelvbamoduleuserform

提问by 4 Leave Cover

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

我怎么能叫UserForm_Initialize()Module,而不是用户窗体代码对象的?

回答by Daniel

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.

Public Sub UserForm_Initialize()所以它可以从表单外部调用。

Alternately, if the Userform hasn't been loaded:

或者,如果用户表单尚未加载:

UserFormName.Showwill end up calling UserForm_Initializebecause it loads the form.

UserFormName.Show最终会调用,UserForm_Initialize因为它加载了表单。

回答by dee

IMHO the method UserForm_Initialize should remain privatebacause it is event handler for Initialize eventof the UserForm.

恕我直言,UserForm_Initialize方法应该保持私有,因为它是UserForm 的Initialize 事件事件处理程序

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.

创建用户窗体的新实例时调用此事件处理程序。在这个偶数处理程序中,您可以初始化 UserForm1 类的私有成员。

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

回答by 4 Leave Cover

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

解决方案 经过这么长时间,我设法解决了这个问题。

In Module: UserForms(Name).Userform_Initialize

在模块中:UserForms(Name).Userform_Initialize

This method works best to dynamically init the current UserForm

此方法最适合动态初始化当前用户窗体