2012-10-24 165 views
0

我有以下几组复选框和广播组,并想知道如何在VBA中单独验证它们。任何人都可以指引我走向正确的方向吗?使用VBA验证复选框和单选按钮组使用VBA

例如我将需要至少复选框在周选定组和每一个无线电按钮组的(优先lecturestyleroomstrucroomtype)只是需要选择以被制造。我将不胜感激任何代码向我展示如何实现这一点。

- 复选框

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

优先 - 单选按钮

priority_y, priority_n

lecturestyle - 单选按钮

lecturestyle_trad,lecturestyle_sem

roomstruc - 单选按钮

roomstruc_tiered,roomstruc_flat

roomtype - 单选按钮

roomtype_lecture,roomtype_lab

+0

看看这里安迪教皇的例子http://www.ozgrid.com/forum/showthread.php?t=49024。它假定您已经使用控件的groupname属性。 – user3357963

回答

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 
+0

'Exit For'可能比使用Goto – user3357963

+0

真实,但我在哪里放我的msgbox呢?把一个布尔值来捕获有效的状态,然后使用它? – nutsch

+0

每当我尝试运行代码时,我都会收到“方法或数据成员未找到错误”。我认为这与'Me.Priority.Controls'有关系吗? – methuselah

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 
    

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