VBA:用户窗体初始化时未命中用户窗体初始化方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16946263/
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
VBA: Userform initialize method not being hit when userform initializes
提问by Alex Barrie
My module code calling the userform:
我的模块代码调用用户窗体:
PreInfo.Show
My userform code:
我的用户表单代码:
Public Sub PreInfo_Initialize()
Dim Invoice, Name, Model, Crank, MyValue1, StrokeL As Variant
'Dim ListBox1 As ListBox
Dim c As Range
Dim oneControl As Object
'Empty Text Boxes and Set Focus
For Each oneControl In PreInfo.Controls
Select Case TypeName(oneControl)
Case "TextBox"
oneControl.Text = vbNullString
'Case "ListBox"
'oneControl.AddItem "Test"
End Select
Next oneControl
With lbTest
.AddItem Item:="2 Cylinders" '3 different syntax used as test to isolate issue
.AddItem "3 Cylinders"
.AddItem ("5 Cylinders")
End With
Invoice.TextBox.SetFocus 'Activate?
End Sub
My module code does not trigger my userform initialize sub, therefore nothing in that sub runs. I cannot figure out why this is happening. I would greatly appreciate any help!
我的模块代码不会触发我的用户窗体初始化子程序,因此该子程序中没有任何内容运行。我无法弄清楚为什么会这样。我将不胜感激任何帮助!
When this code runs, userform pops up, however none of the listbox items are added
当此代码运行时,用户窗体会弹出,但没有添加任何列表框项
采纳答案by Alex Barrie
I have figured it out. Long story short, my Module needed the following code:
我已经想通了。长话短说,我的模块需要以下代码:
Userform.Userform_Activate 'THIS IS THE NEW CODE
Userform.Show 'existing code, unchanged
which signals the userform to activate before it is open (calling "initialize", then showing the userform for the user to alter).
它在用户窗体打开之前发出信号使其激活(调用“初始化”,然后显示用户窗体以供用户更改)。
Userform.Show SHOULD cue this activation sub to run, however mine was not for whatever reason. This fixes the issue until I determine why Userform.Userform_Activate was not called like it should have been.
Userform.Show 应该提示这个激活子运行,但是我的不是出于任何原因。这解决了这个问题,直到我确定为什么 Userform.Userform_Activate 没有像它应该被调用的那样被调用。
回答by FCastro
Userform_Initialize event is triggered by a line like this called in a module:
Userform_Initialize 事件由在模块中调用的这样一行触发:
Load Userform1
In order for it to be triggered again, you'll need to unload the userform (not simply hide it). This can be done either after the load call within the module:
为了再次触发它,您需要卸载用户表单(而不是简单地隐藏它)。这可以在模块内的 load 调用之后完成:
Unload Userform1
Or anywhere within the Userform's code:
或用户表单代码中的任何地方:
Unload Me
Notice that the events Initializeand QueryClosewill be triggered by the Unloadcall as well (QueryCloseis also triggered when the close button on the top right corner is pressed), so I really recommend you refrain from using Initialize. Instead, After the Loadcall, add the initialize code within the same module (or create a separate sub if it'll be called from multiple places).
请注意,事件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
Last but not least, if you're running a Modelessform (let's code run in the background while showing), you'll need to use the Activateevent for the code to run. The event sequence is:
最后但并非最不重要的是,如果您正在运行无模式表单(让我们在显示时在后台运行代码),您将需要使用Activate事件来运行代码。事件顺序是:
- Userform_Initialize, after Load Userform
- Userform_Activate, after Userform.Show
- Userform_QueryClose, after Unload Userform, pressing the closing "X" or terminating via closing Excel/Task Manager
- Userform_Terminate, when it's really gonna end (though I have no clue how this is used).
- Userform_Initialize,加载用户表单后
- Userform_Activate,在Userform.Show之后
- Userform_QueryClose,在卸载 Userform 之后,按下关闭的“X”或通过关闭 Excel/任务管理器终止
- Userform_Terminate,当它真的要结束时(虽然我不知道它是如何使用的)。
回答by Sam Lucas
I had the same problem, and found a very simple solution.
我遇到了同样的问题,并找到了一个非常简单的解决方案。
In your case, instead of using
在你的情况下,而不是使用
Public Sub PreInfo_Initialize()
use
用
Public Sub UserForm_Initialize()
回答by enderland
I use userform.hide when the user clicks the "continue" button on the userform, which closes the userform and prints the userform inputs into a worksheet
当用户单击用户表单上的“继续”按钮时,我使用 userform.hide,这会关闭用户表单并将用户表单输入打印到工作表中
What is happenening is that your userform is never unloaded from memory. Hide
only removes it from view.
发生的事情是您的用户表单永远不会从内存中卸载。Hide
只将其从视图中删除。
This means that it is only initialized the first time you run the userform within that Excel instance.
这意味着它仅在您第一次在该 Excel 实例中运行用户表单时初始化。
You can prevent this by using
您可以通过使用来防止这种情况
unload me
or
或者
End
instead of UserForm.Hide
depending on your other code. You could also potentially use the UserForm_Activate
method instead of UserForm_Initialize
method.
而不是UserForm.Hide
取决于您的其他代码。您也可以潜在地使用UserForm_Activate
方法而不是UserForm_Initialize
方法。
To populate the ListBox, use:
要填充列表框,请使用:
lbTest.AddItem "3 Cylinders"
etc outside the With
statement.
等在With
声明之外。
回答by Iu To
you have to keep syntax UserForm_Initialize() to make it happen
您必须保留语法 UserForm_Initialize() 才能实现
Cheers
干杯