全部复选框不会选中所有其他复选框

问题描述 投票:0回答:1

我通过单击sheet1 中的按钮来调用用户窗体。 UserForm 由 Private Sub UserForm_Initialize() 中的代码创建。用户窗体包含一个由多个复选框填充的框架。这些复选框之一名为“全部”,旨在选择所有其他复选框。但是,当我单击它时,所有其他复选框都保持未选中状态。

我对与每个复选框单击关联的事件进行了编码,对于“全部”复选框、Private Sub All_Click(),我将所有其他复选框值设置为 true,这通常应该勾选它们。但这不起作用。任何指导都会受到欢迎。 到目前为止我的代码:

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
excel vba checkbox frame userform
1个回答
0
投票

如果您手动创建用户表单而不是通过编程方式创建用户表单,那么一切都会正常。

以编程方式创建用户表单时,不会触发单击或更改事件。

看这里:单击事件无法以编程方式/动态创建的选项按钮工作

© www.soinside.com 2019 - 2024. All rights reserved.