Dynamic Control Box controls

supermercadinho

New Member
Joined
Aug 24, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello,

If anyone can help me understand the inner workings of this code, I'd greatly appreciate the help.
VBA Code:
' in code module for clsRunTimeCheckBox

Public WithEvents Checkbox As MSForms.Checkbox

Event Change()

Private Sub Checkbox_Click()
    Set UFParent.ActiveCheckBox = Me
    RaiseEvent Change
End Sub

Property Get UFParent() As Object
    Set UFParent = Checkbox.Parent
    On Error Resume Next
    Do
        Set UFParent = UFParent.Parent
    Loop Until Err
    On Error GoTo 0
End Property

And this piece that works together

Code:
' in userform code module

Dim AddedCheckBoxes As Collection

Public WithEvents ActiveCheckBox As clsRunTimeCheckBox

Private Sub ActiveCheckBox_Change()
    MsgBox ActiveCheckBox.Checkbox.Name
End Sub

Private Sub UserForm_Initialize()
    Dim i As Long
    Dim newBox As clsRunTimeCheckBox
    Set AddedCheckBoxes = New Collection
    For i = 1 To 6
        Set newBox = New clsRunTimeCheckBox
        With newBox
            Set .Checkbox = Me.Controls.Add("forms.CheckBox.1")
            With .Checkbox
                .Caption = "MyBox" & i
                .Width = 150
                .AutoSize = True
                .Top = 5 + 20 * (i - 1)
            End With
        End With
        AddedCheckBoxes.Add Item:=newBox
    Next i
End Sub

This is all in reference to another thread (Dynamic CheckBox control in a VBA UserForm) about dynamically generating check boxes from an array. My own code is very similar to this one but I have it loop through an array of names that I gave it and it works. I want to know how to then find and reference those clicked check boxes so that I can use some sort of logic to filter out data in another series of codes I have running in my macros such as this.

Code:
                If frmGroupProjectsBy.ckbProjPhaseArchive.Value = False And strProjectPhase = "Archive" Then
                    GoTo SkipToNextRow
                Else
                    'Do Nothing
                End If

If anyone can at least help me understand more about the first code posted I'd greatly appreciate it.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top