【问题标题】:VBA: Userform initialize method not being hit when userform initializesVBA:用户窗体初始化时未命中用户窗体初始化方法
【发布时间】:2013-06-01 12:39:09
【问题描述】:

我的模块代码调用用户表单:

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.hide吗?
  • 当用户单击用户表单上的“继续”按钮时,我使用 userform.hide,这会关闭用户表单并将用户表单输入打印到工作表中。

标签: vba module listbox initialization userform


【解决方案1】:

Userform_Initialize 事件由模块中调用的这样一行触发:

Load Userform1

为了再次触发它,您需要卸载用户表单(而不是简单地隐藏它)。这可以在模块内的 load 调用之后完成:

Unload Userform1

或者用户表单代码中的任何地方:

Unload Me

请注意,InitializeQueryClose 事件也将由 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

最后但同样重要的是,如果您正在运行 Modeless 表单(让我们在显示时在后台运行代码),您将需要使用 Activate 事件让代码运行。事件顺序为:

  • Userform_Initialize,在Load Userform之后
  • Userform_Activate,在 Userform.Show 之后
  • Userform_QueryClose,在Unload Userform之后,按关闭“X”或通过关闭Excel/任务管理器终止
  • Userform_Terminate,真正结束的时候(虽然我不知道它是如何使用的)。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,并找到了一个非常简单的解决方案。

    在你的情况下,而不是使用

    Public Sub PreInfo_Initialize()     
    

    使用

    Public Sub UserForm_Initialize()      
    

    【讨论】:

      【解决方案3】:

      我想通了。长话短说,我的模块需要以下代码:

      Userform.Userform_Activate 'THIS IS THE NEW CODE
      Userform.Show 'existing code, unchanged
      

      在用户表单打开之前通知用户表单激活(调用“初始化”,然后显示用户表单以供用户更改)。

      Userform.Show 应该提示这个激活子运行,但是我的不是出于任何原因。这可以解决问题,直到我确定为什么没有像应有的那样调用 Userform.Userform_Activate。

      【讨论】:

        【解决方案4】:

        当用户单击用户表单上的“继续”按钮时,我使用 userform.hide,这会关闭用户表单并将用户表单输入打印到工作表中

        正在发生的事情是您的用户表单永远不会从内存中卸载。 Hide 只会将其从视图中移除。

        这意味着它仅在您第一次在该 Excel 实例中运行用户窗体时才被初始化。

        您可以通过使用来防止这种情况

        unload me
        

        End
        

        而不是UserForm.Hide,具体取决于您的其他代码。您也可以使用UserForm_Activate 方法代替UserForm_Initialize 方法。


        要填充 ListBox,请使用:

        lbTest.AddItem "3 Cylinders"
        

        With 语句之外的等。

        【讨论】:

        • 将语法更改为:unload me 用户窗体出现,但 ListBox 仍未填充。
        • @AlexBarrie 我添加了解决该问题的代码。如果上面的代码不起作用,在那儿放一个断点,看看你的代码是否进入了这个方法。
        • 修改了代码(结果相同)。在代码处添加断点(程序继续超过该点而不触发断点)。就好像 userform.show 有效,但不会触发 userform_activate 子。
        • @AlexBarrie 为了清楚起见,您在与您的用户窗体关联的代码中有此代码?
        • 是的,这个项目有一个模块和两个用户表单(这个麻烦的是第一个。第二个只是文本框,根据需要工作)。 Userform.show 是我模块中的第一行之一,上面的其余代码在 userform 内,在 userform_activate() 子内。这是一个公共潜艇。
        【解决方案5】:

        你必须保持语法 UserForm_Initialize() 才能实现

        干杯

        【讨论】:

        • 我觉得自己像个白痴!这对我来说是个问题。我将 TestForm_Initialize() 用于名为 TestForm 的表单。将其更改为 UserForm_Initialize() 并按预期工作。
        猜你喜欢
        • 1970-01-01
        • 2016-02-02
        • 1970-01-01
        • 1970-01-01
        • 2013-10-24
        • 1970-01-01
        • 1970-01-01
        • 2020-07-21
        • 1970-01-01
        相关资源
        最近更新 更多