Ale*_*rie 6 vba module listbox initialization userform
我的模块代码调用userform:
PreInfo.Show
Run Code Online (Sandbox Code Playgroud)
我的用户格式代码:
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
Run Code Online (Sandbox Code Playgroud)
我的模块代码不会触发我的userform initialize sub,因此该子程序中没有任何内容运行.我无法弄清楚为什么会这样.我非常感谢任何帮助!
此代码运行时,会弹出userform,但不会添加任何列表框项
小智 7
Userform_Initialize事件由模块中调用的这样的行触发:
Load Userform1
Run Code Online (Sandbox Code Playgroud)
为了再次触发它,您需要卸载用户窗体(而不是简单地隐藏它).这可以在模块内的加载调用之后完成:
Unload Userform1
Run Code Online (Sandbox Code Playgroud)
或者Userform代码中的任何位置:
Unload Me
Run Code Online (Sandbox Code Playgroud)
请注意,事件Initialize和QueryClose也将由Unload调用触发(当按下右上角的关闭按钮时也会触发QueryClose),所以我建议您不要使用Initialize.相反,在加载调用之后,在同一模块中添加初始化代码(或者如果它将从多个位置调用,则创建一个单独的子代码).
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
Run Code Online (Sandbox Code Playgroud)
最后但并非最不重要的是,如果您正在运行无模式表单(让代码在显示时在后台运行),则需要使用Activate事件来运行代码.事件序列是:
我已经想通了。长话短说,我的模块需要以下代码:
Userform.Userform_Activate 'THIS IS THE NEW CODE
Userform.Show 'existing code, unchanged
Run Code Online (Sandbox Code Playgroud)
它指示用户窗体在打开之前激活(调用“初始化”,然后显示用户窗体以供用户更改)。
Userform.Show 应提示此激活子程序运行,但我的子程序出于某种原因没有运行。这解决了这个问题,直到我确定为什么没有像应该那样调用 Userform.Userform_Activate 。
| 归档时间: |
|
| 查看次数: |
22821 次 |
| 最近记录: |