Userform not triggering Initialize or Activate eve

2020-03-24 03:42发布

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条回答
冷血范
2楼-- · 2020-03-24 04:31

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

enter image description here

查看更多
登录 后发表回答