title: Excel VBA add handler to every checkbox in form
body: <p>I have an excel form with a large number of checkboxes that are added at runtime. I would like to add a handler to each one of those checkboxes that will run when the value is changed. I know in other versions of Visual Basic I would use AddHandler, but that doesn't work in Excel VBA.</p>
  
  <p>Following an example, I came up with the following code:</p>
  
  <pre><code>'This is in a class module called CheckboxHandler
  Public WithEvents cb As MSForms.CheckBox
  
  Private Sub cb_change()
      MsgBox ("test")
  end sub
  </code></pre>
  
  <p>And, in my userform, I have this code:</p>
  
  <pre><code>With CreateObject("Scripting.Dictionary")
  .....'Unrelated code omitted
  
  'Variable Checkboxes
  'Add Handler to checkboxes
  Dim colCBHandlers As Collection
  Set colCBHandlers = New Collection
  Dim objHandler As CheckboxHandler
  
  Dim i As Long
  Dim chkBox As MSForms.CheckBox
  For i = 1 To .count - 1
      Set chkBox = Me.Controls.Add("Forms.Checkbox.1", "Checkbox" &amp; i)
      chkBox.Caption = .Keys()(i)
      chkBox.VALUE = False
      chkBox.Top = (chkBox.Height + 10) * (i - 1) + 55
      chkBox.Left = 725
      Set objHandler = New CheckboxHandler
      Set objHandler.cb = chkBox
      colCBHandlers.Add objHandler
  Next i
  End With
  </code></pre>