Search code examples
excelvba

Dynamically created multipage with checkbox, how can I trigger a CheckBox1_Change() event?


I created a template multipage page.
enter image description here

This template is then copied with Me.MP.Pages.Item(0).Controls.Copy and pasted Me.MP.Pages(p).Paste the UserForm_Initialize and the labels and listbox is filled with data from the sheet.
When the form is loaded there could be anywhere from 0 to 100 pages.

How can I detect the change in the checkbox?
So far I have only managed to create this code:

Private Sub MP_Change()

    Debug.Print Me.MP.Pages(Me.MP.Value).Controls(3).Value
    
End Sub

This code prints when I change page on the mulipage, but there is no guarantee you change pages. There could be just 1 page.

What I need is to record the checkbox true/false value on a sheet. The above code could do that if you switch pages back and forth.
I could also create a UserForm_Terminate() and iterate all the pages and record it then.
That would work, but I'm looking for the cleaner event method.
Is that even possible?


Solution

  • Please, use the next way to allocate click event to all copied check boxes:

    1. Insert a class module and name it CheckBoxCls, then copy the next simple code in it:
    Option Explicit
    
    Public WithEvents chkBEvent As MSForms.CheckBox
    
    Private Sub chkBEvent_Click()
        MsgBox "Check box from page " & chkBEvent.Parent.name & " was clicked." & vbCrLf & _
               "Its value is: " & chkBEvent.Value
    End Sub
    

    It contains the Click event, able to identify on which MultiPage page the clicked check box exists and what value it has. Please, adapt it to do what you really need it to do.

    1. Copy the next declaration on top of the User Form code module (in the declarations area):
     Private chkB() As New CheckBoxCls
    
    1. I tested this solution using the next (simple) example code to also allocate the class event to all copied check boxes (after controls copying). You should adapt your existing code in this way, or even keep it as it is (instead of yours) if yours does not also do something else:
    Dim p As Long, ctrl As MSForms.Control, k As Long
    
       Me.MultiPage1.Pages.item(0).Controls.Copy
       
       ReDim chkB(Me.Controls.count) 'an initial maximum number of controls
       For p = 1 To Me.MultiPage1.Pages.count - 1 'start copying from the second page
        Me.MultiPage1.Pages(p).Paste
        For Each ctrl In Me.MultiPage1.Pages(p).Controls
            If TypeOf ctrl Is MSForms.CheckBox Then
                Set chkB(k).chkBEvent = ctrl: k = k + 1: Exit for 'allocate event
            End If
         Next ctrl
       Next p
       
       ReDim Preserve chkB(k - 1) 'keep only loaded check boxes
    

    Now, play with ticking/un-ticking each involved check box and see how it works.

    Note: Your first check box must have its own event already created. If not, it is not complicated to also add it automatically.

    If something not clear, plese do not hesitate to ask for clarifications.