Userform not triggering Initialize or Activate event

走远了吗. 提交于 2019-12-19 07:39:07

问题


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.

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


回答1:


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

or

Private Sub UserForm_Activate()

End Sub

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



来源:https://stackoverflow.com/questions/19401905/userform-not-triggering-initialize-or-activate-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!