0

I wrote sub which add checkbox like below

Sub WstawCHB(i As Integer, ByVal ws As Worksheet)
Dim NewCheckBox As MSForms.CheckBox

Set NewCheckBox = ListaObecnosciForm.Controls.Add("Forms.Checkbox.1", "CB" & i, True)
With NewCheckBox
    .Top = 20 * i
    .Left = 20
    .Width = 450
    .Height = 24
    .Caption = ws.Cells(2 + i, 27)
    .Value = False
End With

End Sub

How to add some action to the checkbox in code. For example (.OnAction = "CheckBox1_Click")

Private Sub CheckBox1_Click()
  MsgBox "Hello World!"
End Sub

Additional information: the Sub is calling in loop in code below, so I have to "inject" code for every created checkbox for each iteration of loop

Sub DodajCHB(ByVal LW As Integer, ByVal ws As Worksheet)

Dim i As Integer
i = 1
Do While i < LW
    Call WstawCHB(i, ws)
    i = i + 1
Loop

End Sub
gilange
  • 1
  • 1
  • [How to add events to dynamically created controls (buttons, listboxes) in Excel VBA userform](https://stackoverflow.com/questions/48382957/how-to-add-events-to-dynamically-created-controls-buttons-listboxes-in-excel) – Darren Bartrup-Cook Jun 30 '22 at 10:17
  • Thanks Darren. Now I have a problem with setting some peperty in the class "clsMyEvents" which allow these variables to be read from and written to (Property Get Property Let functions or Property Set for object type variables). Is it possible to do this with this type of class? – gilange Jul 04 '22 at 11:18
  • Yes, it's possible. `Public Property Get SomeProperty() As String: SomeProperty = pSomeProperty: End Property` and `Public Property Let SomeProperty(Value As String): pSomeProperty = Value: End Property`. This might help: http://www.cpearson.com/excel/classes.aspx – Darren Bartrup-Cook Jul 04 '22 at 11:54
  • Does this answer your question? [How to add events to dynamically created controls (buttons, listboxes) in Excel VBA userform](https://stackoverflow.com/questions/48382957/how-to-add-events-to-dynamically-created-controls-buttons-listboxes-in-excel) – Ike Jul 06 '22 at 20:45

0 Answers0