Userform_Initialize 事件由模块中调用的这样一行触发:
Load Userform1
为了再次触发它,您需要卸载用户窗体(而不是简单地隐藏它)。这可以在模块内的 load 调用之后完成:
Unload Userform1
或者用户表单代码中的任何地方:
Unload Me
请注意,事件Initialize和QueryClose也将由Unload调用触发(当按下右上角的关闭按钮时也会触发 QueryClose),所以我真的建议您不要使用Initialize。相反,在Load调用之后,在同一个模块中添加初始化代码(或者如果它会从多个位置调用,则创建一个单独的子)。
Sub LoadThatUserform
Load Preinfo
'All textboxes are loaded with their value set to vbnullstring, _
unless you specified otherwise in the Properties box.
With ThatUserform.lbTest
'Answering this test
.AddItem Item:="2 Cylinders" 'Here you used the parameter name. _
It's entirely optional, which is why the one below _
also works. It's necessary, however, if you wanna skip _
an optional parameter on a procedure call.
.AddItem "3 Cylinders"
.AddItem ("5 Cylinders") 'This will theoretically create a _
run-time error: a procedure call either outside of a Call _
statement or not setting a value to a variable or property _
doesn't require parentheses.
End With
'After loading, show the form
Preinfo.Show
'Showing, if not as modeless, stops code execution for the user _
to make changes to the form. Once he presses a button _
or whatever, and the form is hidden, code will resume. _
After you grab every form data you need, just call Unload.
Unload Preinfo
End Sub
最后但同样重要的是,如果您正在运行无模式表单(让代码在显示时在后台运行),您将需要使用激活事件来运行代码。事件顺序为:
- Userform_Initialize,在加载用户表单之后
- Userform_Activate,在Userform.Show之后
- Userform_QueryClose,在Unload Userform之后,按关闭“X”或通过关闭 Excel/任务管理器终止
- Userform_Terminate,当它真的要结束时(尽管我不知道它是如何使用的)。