0

我有以下几组复选框和无线电组,想知道如何在 VBA 中分别验证它们。谁能指出我正确的方向?

例如,我要求在组中至少选中复选框,并且每个单选按钮组(优先级演讲风格、房间结构房间类型)只需要进行选择。我将不胜感激任何代码向我展示如何完成此操作。

- 复选框

chk_week1, chk_week2, chk_week3, chk_week4, chk_week5,..., chk_week15

优先级- 单选按钮

优先级_y,优先级_n

演讲风格- 单选按钮

Lecturestyle_trad,lecturestyle_sem

roomstruc - 单选按钮

roomstruc_tiered, roomstruc_flat

房间类型- 单选按钮

roomtype_lecture, roomtype_lab

4

2 回答 2

1

你可以尝试这样的事情。命名帧优先级可能会返回奇怪的结果,所以我重命名了你的帧frPriorityfrLectureStyle

Sub cbValidate_Click()
Dim ctl As Control, strOption As String, lLoop As Long

For lLoop = 1 To 15
    If Me.Controls("chk_week" & lLoop).Value = True Then GoTo nxtCheck0
Next

MsgBox "You didn't select a week"

nxtCheck0:

For Each ctl In Me.frPriority.Controls
    If ctl.Value = True Then GoTo nxtCheck1
Next

MsgBox "You didn't select a priority"

nxtCheck1:

For Each ctl In Me.frLectureStyle.Controls
    If ctl.Value = True Then GoTo nxtCheck1
Next

MsgBox "You didn't select a lecture Style"

nxtCheck1:

Unload Me
End Sub
于 2012-10-24T23:58:41.393 回答
1

您可以尝试类似下面的编码,因为当您在组中有许多控件时它会变得乏味:

  1. 插入一个名为“MyForm”的用户表单,其中包含名为“chk01”、“chk02”...的复选框。”chk20 或任何你想要的复选框

  2. 将以下声明放入代码模块(不是表单模块)

    Public CheckBoxGroup(1 To 20) As New MyForm
    
  3. 将以下声明放入表单代码中

    Public WithEvents ChkBoxGrp As CheckBox
    
  4. 以激活事件的形式初始化数组

    Private Sub UserForm_Activate()
    Dim i As Integer
        For i = 1 To 20 (or however many checkboxes you have
                set CheckBoxGroup (i).ChkBoxGrp = MyForm("chk" + Right("0" + CStr(i), 2))
        Next i
    End Sub
    
    Private Sub ChkBoxGrp_Click()
        Select Case val(Right(ChkBoxGrp.Name, 2))
                Case 1
                ‘Do whaever you want for chk01 box
            Case 2
                ‘Do whaever you want for chk01 box
            ……………
            End Select
    End Sub
    

您可以对其他复选框事件使用相同的事件处理

于 2016-08-17T17:17:02.240 回答