Reputation: 1
I call a UserForm by clicking a button in sheet1. The UserForm is created by the code in Private Sub UserForm_Initialize(). The UserForm contains a frame populated by several checkboxes. One of these checkboxes, named All, intends to select all the other checkboxes. However, when I click it all the other checkboxes remain unchecked.
I coded the events associated with each checkbox click and for the All checkbox, Private Sub All_Click(), I set all other checkboxes values to true which normally should tick them. But it does not work. Any guidance would be more than welcomed. My code so far:
Option Explicit
Private Sub UserForm_Initialize()
Dim myFrame As MSForms.Frame
Dim myCheckBox As MSForms.CheckBox
Dim topPosition As Long
Dim checkBoxNames As Variant
Dim i As Long
Dim txtBox As MSForms.TextBox
Dim lbl As MSForms.Label
'Set width and height of UserForm SMA
With Me
.Width = 270
.Height = 240
.ForeColor = &H464646 'Dark Gray
.BackColor = &HFFFFFF 'White
.BorderColor = &HA9A9A9 'Light Gray
End With
'Create the Frame
Set myFrame = Me.Controls.Add("Forms.Frame.1", "SMA", True)
With myFrame
.Left = 25
.Top = 5
.Width = 100
.Height = 190
.Caption = "Select SMA"
.ForeColor = &H464646 'Dark Gray
.BackColor = &HFFFFFF 'White
.BorderStyle = fmBorderStyleSingle
.BorderColor = &HA9A9A9 'Light Gray
End With
'Define checkbox names
checkBoxNames = Array("SMA10", "SMA20", "SMA50", "SMA100", "SMA200", "All")
'Initialize top position for checkboxes
topPosition = 10
'Add checkboxes to the frame
For i = LBound(checkBoxNames) To UBound(checkBoxNames)
Set myCheckBox = myFrame.Controls.Add("Forms.CheckBox.1", checkBoxNames(i), True)
With myCheckBox
.Left = 25
.Top = topPosition
.Width = 100
.Height = 20
.Caption = checkBoxNames(i)
.AutoSize = True
.ForeColor = &H464646 'Dark Gray
.BackColor = &HFFFFFF 'White
End With
topPosition = topPosition + 30
Next i
'Uncheck the checkboxes
With myFrame
.SMA10.Value = False
.SMA20.Value = False
.SMA50.Value = False
.SMA100.Value = False
.SMA200.Value = False
.All.Value = False
End With
'Add textbox for user input
Set txtBox = Me.Controls.Add("Forms.TextBox.1", "txtUserChoice", True)
With txtBox
.Left = 140
.Top = 50
.Height = 20
.AutoSize = False
.Font.Size = 10
.ForeColor = &H464646 'Dark Gray
.BackColor = &HFFFFFF 'White
.BorderStyle = fmBorderStyleSingle
.BorderColor = &HA9A9A9 'Light Gray
End With
'Add label for textbox
Set lbl = Me.Controls.Add("Forms.Label.1", "lblUserChoice", True)
With lbl
.Left = 140
.Top = 30
.Caption = "Or enter number of periods:"
.AutoSize = True
.WordWrap = False
.TextAlign = fmTextAlignLeft
.Font.Name = ("Arial Narrow")
.Font.Size = 10
.ForeColor = &H464646 'Dark Gray
.BackColor = &HFFFFFF 'White
.BorderStyle = fmBorderStyleNone
End With
'Create OK and Cancel buttons
Dim btnOK As MSForms.CommandButton
Dim btnCancel As MSForms.CommandButton
'Create button OK
Set btnOK = Me.Controls.Add("Forms.CommandButton.1", "btnOK", True)
With btnOK
'.Name = "btnOK"
.Left = 150
.Top = 90
.Width = 80
.Height = 25
.Caption = "OK"
'.Font = 10
.Font.Name = "Arial Narrow"
.ForeColor = &H464646 'Dark Gray
.BackColor = &HFFFFFF 'White
.Enabled = True
End With
'Create button Cancel
Set btnCancel = Me.Controls.Add("Forms.CommandButton.1", "btnCancel", True)
With btnCancel
'.Name = "btnCancel"
.Left = 150
.Top = 125
.Width = 80
.Height = 25
.Caption = "Cancel"
'.Font = 10
.Font.Name = "Arial Narrow"
.ForeColor = &H464646 'Dark Gray
.BackColor = &HFFFFFF 'White
.Enabled = True
End With
End Sub
Private Sub SMA10_Click()
'Assign value 10 when SMA10 checkbox is checked
If myFrame.SMA10.Value = True Then
myFrame.SMA10.Value = 10
End If
End Sub
Private Sub SMA20_Click()
'Assign value 20 when SMA20 checkbox is checked
If myFrame.SMA20.Value = True Then
myFrame.SMA20.Value = 20
End If
End Sub
Private Sub SMA50_Click()
'Assign value 50 when SMA50 checkbox is checked
If myFrame.SMA50.Value = True Then
myFrame.SMA50.Value = 50
End If
End Sub
Private Sub SMA100_Click()
'Assign value 100 when SMA100 checkbox is checked
If myFrame.SMA100.Value = True Then
myFrame.SMA100.Value = 100
End If
End Sub
Private Sub SMA200_Click()
'Assign value 200 when SMA200 checkbox is checked
If myFrame.SMA200.Value = True Then
myFrame.SMA200.Value = 200
End If
End Sub
Private Sub All_Click()
'If checkboxAll is checked, set other checkboxes to be checked
If myFrame.All.Value = True Then
With myFrame
.SMA10.Value = True
.SMA20.Value = True
.SMA50.Value = True
.SMA100.Value = True
.SMA200.Value = True
End With
MsgBox "All checkboxes set to True."
Else
MsgBox "Checkbox All is not checked."
End If
End Sub
Upvotes: 0
Views: 79
Reputation: 76
Click or change events are not triggered when you create userform programmatically. If you create userform manually, they'll work. It'll be faster.
If you want to create userform programmatically anyway, look here: click event not working on programmatically / dynamically created optionbutton
Upvotes: 0