2015-07-06 66 views
0

我已经创建了一个带有一些复选框的框架的用户窗体。在框架旁边,我有按钮。流程就像我必须选择复选框,然后点击按钮来获取一些数据。选择VBA框中存在的复选框不会触发任何事件

但我想要做的是,我不希望按钮被启用,直到我选择框架中的任何复选框。我使用“输入/点击”事件程序,他们都没有工作。

以下是我的尝试,仍然没有启用Commandbutton2

任何人都可以请帮我解决这个问题吗?

Private Sub Frame1_enter() 

For Each C In Frame1.Controls 

If TypeOf C Is MSForms.CheckBox Then 

    If C.Value = True Then 
    Me.CommandButton2.Enabled = True 
    Exit Sub 

End If 
End If 
Next 
End Sub 

回答

0

我知道这是有点旧,但这里是一个解决方案。不幸的是,您需要分别为每个Checkbox处理Change事件。至少可以将逻辑分解为单独的方法。

Private Sub CheckBox1_Change() 
    SetButtonEnabled 
End Sub 

Private Sub CheckBox2_Click() 
    SetButtonEnabled 
End Sub 

Private Sub CheckBox3_Change() 
    SetButtonEnabled 
End Sub 

Sub SetButtonEnabled() 
    Me.CommandButton2.Enabled = ValidateChecks 
End Sub 

Function ValidateChecks() As Boolean 

    Dim cbx As MSForms.CheckBox 
    Dim ctl As Control 
    Dim bSomethingChecked As Boolean 

    For Each ctl In Me.Frame1.Controls 

    If TypeName(ctl) = "CheckBox" Then 
     Debug.Print "Found checkbox : " & ctl.Name 
     Set cbx = ctl 
     If cbx.Value Then 
     bSomethingChecked = True 
     Exit For 
     End If 
    Else 
     Debug.Print ctl.Name & " is not a checkbox: " & ctl.Name 
    End If 
    Next 
    Set cbx = Nothing 
    Set ctl = Nothing 
    ValidateChecks = bSomethingChecked 
End Function