用于说明用户表单中所有复选框的代码?

Code to account for all checkboxes in a userform?

我在包含多个复选框和多个 DTPickers 的用户表单上有代码。

代码如下所示:

Private Sub CheckBox11_Click()
If CheckBox11.Value = True Then
    DTPicker22.Enabled = True
Else
    DTPicker22.Enabled = False
End If
End Sub

Private Sub CheckBox12_Click()
If CheckBox12.Value = True Then
    DTPicker24.Enabled = True
Else
    DTPicker24.Enabled = False
End If
End Sub 

用户表单包含许多旁边有子句的复选框。完成后,DTPicker 将允许输入完成日期。

虽然这符合我的要求,但它只在每个私有子项勾选复选框时启用一个 DTPicker。必须有一些方法可以做到这一点,这样我就不需要为每个复选框点击事件创建不同的私人订阅。

你能告诉我把它放在哪里吗,比如什么事件?

我建议的第一件事是遵循正确的命名约定。 “CheckBox11”和“DTPciker1”真的很模糊,一旦你深入研究你的代码,你就会忘记哪个控件是哪个。我建议将它们命名为将两个控件关联在一起的名称,例如“firstDate”和“firstDateDTP”。我下面的替代答案使用这种方法。

您可以创建一个 public 函数,根据复选框的值启用 DTPicker。

Public Function EnableDTPicker(myPicker as String, enableBool as Boolean)

    UserFormName.Controls(myPicker).Enabled = enableBool

End Function

然后,您可以像这样在 CheckBox123_Click() 子程序中调用该函数:

Private Sub CheckBox123_Click()
    EnableDTPicker("thePickerName", CheckBox123.Value)
End Sub

或者,您可以制作一个计时器事件,该事件运行 x 秒数,只循环控制并根据需要执行检查。 See this page on how to set up the timer. 使用显示的 link 中的代码,您可以按照以下方式做一些事情:

'Put this in Workbook events
Private Sub Workbook_Open()
    alertTime = Now + TimeValue("00:00:01")
    Application.OnTime alertTime, "EventMacro"
    UserForm1.Show
End Sub

'Put this in a Module
Public Sub EventMacro()
    With UserForm1
        For each ctrl in .Controls
            If TypeName(ctrl) = "CheckBox" Then
                'The code below assumes the naming convention outlined above is followed
                .Controls(ctrl.Name & "DTP").Enabled = ctrl.Value
            End If
        Next ctrl
    End With

    alertTime = Now + TimeValue("00:00:01")
    Application.OnTime alertTime, "EventMacro"
End Sub

“控制数组”是处理此类问题的典型方法。

见: http://www.siddharthrout.com/index.php/2018/01/15/vba-control-arrays/

例如:

Class 模块 clsEvents

Option Explicit

'Handle events for a checkbox and a date control, associated with a worksheet cell
Private WithEvents m_CB As MSForms.CheckBox
Private WithEvents m_DP As DTPicker
Private m_dateCell As Range

'set up the controls and the cell
Public Sub Init(cb As MSForms.CheckBox, dp As DTPicker, rng As Range)
    
    Set m_CB = cb
    Set m_DP = dp
    Set m_dateCell = rng
    
    If rng.Value > 0 Then
        cb.Value = True
        m_DP.Value = rng.Value
    Else
        cb.Value = False
    End If
    
    m_DP.CustomFormat = "dd/MM/yyyy"
    
End Sub

Private Sub m_CB_Change()
    m_DP.Enabled = (m_CB.Value = True)
End Sub

Private Sub m_DP_Change()
    m_dateCell.Value = m_DP.Value 'update the cell
End Sub

用户表单:

Option Explicit

Dim colObj As Collection 'needs to be a Global to stay in scope

Private Sub UserForm_Activate()
    Dim obj As clsEvents, i As Long, ws As Worksheet
    
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    
    Set colObj = New Collection
    'loop over controls and create a class object for each set
    ' 3 pairs of controls on my test form...
    For i = 1 To 3
        Set obj = New clsEvents
        obj.Init Me.Controls("CheckBox" & i), _
                  Me.Controls("DTPicker" & i), _
                  ws.Cells(i, "B")
        colObj.Add obj
    Next i

End Sub