In the "test" worksheet, I have two ActiveX button with name btTest1 and btTest2.
In the "test" worksheet module:
Dim MyButtons As Collection
Private Sub Worksheet_Activate()
Set MyButtons = New Collection
For i = 1 To 2
Set ButtonClass = New ClassTest
If i = 1 Then Set ButtonClass.obj1 = ActiveSheet.btTest1
If i = 2 Then Set ButtonClass.obj1 = ActiveSheet.btTest2
MyButtons.Add ButtonClass
Next
End Sub
In the ClassTest class module:
Private WithEvents bt As MSForms.CommandButton
Property Set obj1(b As MSForms.CommandButton)
Set bt = b
End Property
Private Sub bt_Click()
MsgBox bt.Caption
End Sub
After I activate the "test" sheet, then click the "TEST 1" button it shows the msgbox "TEST 1" and when I click the "TEST 2" button it shows the msgbox "TEST 2".
How to code so when "test" sheet is activated then the msgbox "TEST 1" directly shows as if there is a user literally click the "TEST 1" button?
You can simply add:
Me.btTest1.Value = True
to the end of your event code.