vba 用户窗体不触发初始化或激活事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19401905/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:50:56  来源:igfitidea点击:

Userform not triggering Initialize or Activate event

vbaexcel-vbauserformexcel

提问by saiki4116

I kept a userform control button in my worksheet to fire up a macro, which in turn shows a user form, In the form I wish to display the opened files in checkboxes(using the Workbooks collection).I wish to run a macro that performs action for the user selected files only.

我在我的工作表中保留了一个用户窗体控件按钮来启动一个宏,它依次显示一个用户窗体,在窗体中我希望在复选框中显示打开的文件(使用工作簿集合)。我希望运行一个宏来执行仅对用户选择的文件执行操作。

So for the button in my worksheet, I have assigned the following macro

所以对于我的工作表中的按钮,我分配了以下宏

Private Sub Button2_Click()

    Load MyForm

    MyForm.Show

End Sub 

At first I kept the below code in the module where my macro sub is there.Since it's not working, I right clicked on user form and selected view code and kept the below code there.But still it's showing the same static designed user form, not the dynamic.I kept breakpoint at both load Myform and MYform.Show() and I stepped through code.It never went into intialize or activate method at all.

起初我将下面的代码保留在我的宏子所在的模块中。由于它不起作用,我右键单击用户表单并选择视图代码并将下面的代码保留在那里。但它仍然显示相同的静态设计用户表单,不是动态的。我在 load Myform 和 MYform.Show() 处都设置了断点,我逐步完成了代码。它根本没有进入初始化或激活方法。

Private Sub MyForm_Activate()
  'for checking the whether this method is called or not I am trying to change caption
  MyForm.LabelSelectFile.Caption = "dhfdfldkfldzjf;zdfkz;d"

  Dim mymyWorkBook As Workbook
  For Each mymyWorkBook In Workbooks
     'code for creating checkbox based on the file name displayed by the workbook collection      
  Next mymyWorkBook
End Sub

I can't understand why that event is not getting triggered.Please help me to overcome this.Thanks in advance

我不明白为什么那个事件没有被触发。请帮我解决这个问题。提前谢谢

回答by Siddharth Rout

Even though the name of the form is MyForm, you still need to use userform.

即使表单的名称是MyForm,您仍然需要使用userform.

'~~> in your worksheet
Private Sub Button2_Click()
    MyForm.Show
End Sub

'~~> In the userform code area
Private Sub UserForm_Initialize()
    '~~> Your code here
End Sub

or

或者

Private Sub UserForm_Activate()

End Sub

The best is to always select the event from the drop down, rather than typing it

最好总是从下拉列表中选择事件,而不是输入它

enter image description here

在此处输入图片说明