【问题标题】:Code to account for all checkboxes in a userform?解释用户表单中所有复选框的代码?
【发布时间】:2021-09-23 19:02:21
【问题描述】:

我在一个用户窗体上有代码,其中包含几个复选框和几个 DTPicker。

代码如下:

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。必须有某种方法可以做到这一点,所以我不需要为每个复选框单击事件创建不同的私有子。

你能告诉我把它放在哪里吗,比如在什么活动中?

【问题讨论】:

标签: vba loops date checkbox userform


【解决方案1】:

“控制数组”是此类事情的典型方法。

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

例如:

类模块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

【讨论】:

  • 完美这个完美地封装了一切,太棒了!下次我得自己去学了,谢谢!!
【解决方案2】:

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

您可以根据复选框的值创建一个启用 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. 使用所示链接中的代码,您可以执行以下操作:

'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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多