I have a userform that dynamically creates checkboxes based on certain worksheet cells having data. My goal is to be able to "check" the checkbox to indicate completion, and have it highlight the corresponding cell. I am looking for guidance on how to accomplish this, since the checkboxes are created dynamically, and the control name and associated cell will vary accordingly.
The control is created with this:
Additional code in the userform module includes:
I have a class module with the following:
The class module and related userform code is based on this post: Dynamic CheckBox control in a VBA UserForm | MrExcel Message Board.
The code works, except not the desired way. As structured, when the useform is activated (or initialized), the change event triggers based on the selected cells being yellow. What I need is to be able to run code when the checkbox is selected (clicked), not when it is changed (which fires on activation or initialization). Experimenting with the class module, I'm finding that there does not seem to be a "click" event option, only the change event.
I also looked at this thread on Stack Overflow. I kind of grasp the concept of a class module, and the idea that I need to incorporate that into the userform, but I'm so far striking out with my experimentation.
Having just succeeded in dynamic control creation for the first time, now encountering collections and class modules is a but overwhelming. I would appreciate any guidance, or alternative suggestions on how to accomplish this. For example, I considered the idea of dynamically adding code to the module for each control, but a brief perusal of the interwebs suggest that is not a very effective solution.
The control is created with this:
VBA Code:
Dim newBox As clsRunTimeCheckBox
Set AddedCheckBoxes = New Collection
Set newBox = New clsRunTimeCheckBox
With newBox
Set .Checkbox = Me.Controls.Add("Forms.Checkbox.1")
With .Checkbox
.Top = 7 + offsetHeight
.Left = 80
.Width = 30
If rCell.Interior.color = vbYellow Then
.value = True
End If
End With
End With
AddedCheckBoxes.Add Item:=newBox
Additional code in the userform module includes:
VBA Code:
Dim AddedCheckBoxes As Collection
Public WithEvents ActiveCheckbox As clsRunTimeCheckBox
Private Sub ActiveCheckbox_Change()
MsgBox ActiveCheckbox.Checkbox.Name
End Sub
I have a class module with the following:
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
The class module and related userform code is based on this post: Dynamic CheckBox control in a VBA UserForm | MrExcel Message Board.
The code works, except not the desired way. As structured, when the useform is activated (or initialized), the change event triggers based on the selected cells being yellow. What I need is to be able to run code when the checkbox is selected (clicked), not when it is changed (which fires on activation or initialization). Experimenting with the class module, I'm finding that there does not seem to be a "click" event option, only the change event.
I also looked at this thread on Stack Overflow. I kind of grasp the concept of a class module, and the idea that I need to incorporate that into the userform, but I'm so far striking out with my experimentation.
Having just succeeded in dynamic control creation for the first time, now encountering collections and class modules is a but overwhelming. I would appreciate any guidance, or alternative suggestions on how to accomplish this. For example, I considered the idea of dynamically adding code to the module for each control, but a brief perusal of the interwebs suggest that is not a very effective solution.