我使用以下代码创建了一个带有动态复选框的用户表单。我正在努力弄清楚如何在底部(或任何地方)添加select all / unselect all复选框,因为列表是动态的。任何帮助将不胜感激。
Private Sub UserForm_Initialize()
'Create Checkboxes form with director names
Dim lRow As Long
Dim i As Long
Dim chkBox As MSForms.CheckBox
lRow = Cells(Rows.Count, 2).End(xlUp).Row
Sheets("Directors_Database").Activate
For i = 4 To lRow
Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
chkBox.Caption = Worksheets("Directors_Database").Cells(i, 2).Value
chkBox.Left = 5
chkBox.Top = 5 + ((i - 2) * 20)
chkBox.Width = 350
Next i
Set chkBox = Nothing
End Sub
答案 0 :(得分:1)
你应该创建"选择全部"复选框在设计时,而不是在运行时 - 否则分配一个事件 - 例程(见this discussion)
有点棘手您可以通过设置top
属性(就像您已经设置的那样)将SelectAll-checkBox放在底部。
然后将Click事件分配给该复选框:
Private Sub CheckBoxSelectAll_Click()
Dim c As Control
For Each c In Me.Controls
If TypeOf c Is MSForms.CheckBox Then
c.Value = CheckBoxSelectAll.Value
End If
Next c
End Sub