我的模块代码调用用户表单:
PreInfo.Show
我的用户表单代码:
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
我的模块代码不会触发我的用户表单初始化子程序,因此该子程序中没有任何内容运行。我无法弄清楚为什么会这样。我将不胜感激任何帮助!
当此代码运行时,会弹出用户窗体,但是没有添加任何列表框项目
最佳答案
我已经想通了。长话短说,我的模块需要以下代码:
Userform.Userform_Activate 'THIS IS THE NEW CODE
Userform.Show 'existing code, unchanged
它指示用户表单在打开之前激活(调用“初始化”,然后显示用户表单供用户更改)。
Userform.Show 应该提示这个激活子运行,但是我的不是出于任何原因。这解决了这个问题,直到我确定为什么 Userform.Userform_Activate 没有像应有的那样被调用。
关于VBA:用户窗体初始化时未命中用户窗体初始化方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16946263/