the following code create three userforms, each one containing a commandbutton defined in a class module and, when clicked on an userform, it should cycle over all controls of that userform. How can I understand on which userform the user has clicked the commandbutton? I don't know if there is a simple solution or it can be done for example using API functions.
this is the code in the userform named Userform1 (add the commandbutton to the userform):
Private Sub UserForm_initialize()
Dim cb_class As Class_CmdButton
Dim cb_cmd As MSForms.CommandButton
Set cb_cmd = Me.Controls.Add("forms.commandbutton.1", "CommandButton1")
cb_cmd.Move 50, 50, 120, 30
Set cb_class = New Class_CmdButton
Set cb_class.CmdButton = cb_cmd
CDict.Add cb_class, cb_cmd
End Sub
this is the code in a standard module (create the userforms):
Sub openuserform()
Dim MyUF As Object
Dim MyUFname As String
Set CDict = CreateObject("Scripting.dictionary")
For i = 1 To 3
Set MyUF = ActiveWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
MyUFname = MyUF.Name
VBA.UserForms.Add (MyUFname)
Set MyUF = New UserForm1
MyUF.Show vbModeless
Next
End Sub
and this is the code in the class module named Class_CmdButton (where i have the problem)
Private WithEvents CMB As MSForms.CommandButton
Private ctrl As Control
Private UF As UserForm
Public Property Set CmdButton(ByVal cmd As MSForms.CommandButton)
Set CMB = cmd
CMB.Caption = "Caption CmdButton"
End Property
Private Sub CMB_Click()
Set UF= 'HOW CAN I GET ACTIVE USERFORM?
For Each ctrl In UF.Controls
MsgBox F.Caption, ctrl.Name
Next
End Sub
thanks in advance for your help
Bookmarks