使用VBAvalidationcheckbox组和单选button

我有以下几组checkbox和广播组,想知道如何在VBA中分别validation它们。 有人能指出我的方向吗?

例如,我会要求至less在星期几组中选中checkbox,每个单选button组( 优先级lecturestyleroomstrucroomtype )只需要进行select。 我将不胜感激任何代码,告诉我如何做到这一点。

星期 – checkbox

chk_week1,chk_week2,chk_week3,chk_week4,chk_week5,…,chk_week15

优先 – 单选button

priority_y,priority_n

讲座式 – 单选button

lecturestyle_trad,lecturestyle_sem

房间结构 – 单选button

roomstruc_tiered,roomstruc_flat

房型 – 单选button

roomtype_lecture,roomtype_lab

你可以尝试这样的事情。 命名帧优先级可能会返回奇怪的结果,所以我已经重命名了您的帧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 

你可能会尝试类似下面的代码,因为当你在组中有很多控件时,它会变得单调乏味:

  1. 插入一个名为“MyForm”的checkbox,名为“chk01”,“chk02”…“。”chk20或checkbox

  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 

您可以对其他checkbox事件使用相同的事件处理