用户表单未触发初始化或激活事件 [英] Userform not triggering Initialize or Activate event

查看:102
本文介绍了用户表单未触发初始化或激活事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在工作表中保留了一个用户窗体控制按钮以启动一个宏,该宏又显示一个用户窗体,在该窗体中,我希望在复选框中显示打开的文件(使用Workbooks集合).我希望运行一个宏,仅对用户选择的文件执行操作.

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 

首先我将下面的代码保存在宏子模块所在的模块中,由于它不起作用,所以我右键单击用户窗体并选择了视图代码,并将下面的代码保留在那里,但仍显示相同的静态设计用户窗体,而不是动态窗体.我在加载Myform和MYform.Show()时都保持断点,并逐步执行了代码.它从来没有进入初始化或激活方法.

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.

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

推荐答案

即使表单名称为MyForm,您仍需要使用userform.

Even though the name of the form is MyForm, you still need to use 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

Private Sub UserForm_Activate()

End Sub

最好是始终从下拉列表中选择事件,而不是键入事件

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

这篇关于用户表单未触发初始化或激活事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆