如何在Excel中一次只允许一个ToggleButton为ON

时间:2015-11-21 18:38:18

标签: excel vba togglebutton

我在excel 2010中创建了一个UserForm,并在其中放入了10个切换按钮。当我运行表单时,所有这些按钮都可以为ON(值= TRUE),但我只希望其中一个同时打开。如下所示:

在课程模块中

Public WithEvents ToggleGroup As ToggleButton 

Private Sub ToggleGroup_click() 

' code

End Sub 

在UserForm中

Dim Buttons() As New Class1 

Private Sub UserForm_Initialize() 

    Dim ToggleCount     As Integer 
    Dim Ctl             As Control 

    For Each Ctl In UserForm1.Controls 
        If TypeName(Ctl) = "ToggleButton" Then 
            ReDim Preserve Buttons(1 To ToggleCount) 
            Set Buttons(ToggleCount).ToggleGroup = Ctl 
        End If 
    Next Ctl 

End Sub

在模块中

Sub Form()
UserForm1.Show
End Sub

5 个答案:

答案 0 :(得分:0)

要使用切换按钮,您可以尝试以下方法:

创建许多切换控件并使用标记属性对按钮进行分组 然后,下面的代码将比较每个控件的名称和标签。具有相同标签的所有切换按钮将与所单击的切换按钮相反。

Private Sub Toggle1_Click()
    ToggleClick Me.ActiveControl
End Sub

Private Sub Toggle2_Click()
    ToggleClick Me.ActiveControl
End Sub

Private Sub Toggle3_Click()
    ToggleClick Me.ActiveControl
End Sub

Private Sub Toggle4_Click()
    ToggleClick Me.ActiveControl
End Sub

Private Sub ToggleClick(ClickedControl As Control)

    Dim Ctl As Control

    For Each Ctl In Me.Controls
        If TypeName(Ctl) = "ToggleButton" Then
            If Ctl.Name <> ClickedControl.Name And Ctl.Tag = ClickedControl.Tag Then
                Ctl.Value = Not ClickedControl.Value
            End If
        End If
    Next Ctl

End Sub

答案 1 :(得分:0)

达伦的答案很有效,除了一个烦恼:当您设置切换控件的值时,Excel(至少2016)会触发“_Click”事件。烦人而且不明显(如果不是一个彻头彻尾的bug,我甚至称之为“勘误”),所以我会做出以下补充:

  1. 将行If Me.ToggleControlName.Value = False then Exit Sub添加为每个“_Click”事件处理程序的第一行。
  2. 将条件And Ctl.Value = True添加到ToggleClick子例程的IF语句中,以防止实际没有更改切换值的“_Click”事件的额外触发(如果它是False,则不需要将其设置为False,触发一个无用的_Click事件处理程序)。

答案 2 :(得分:0)

即使使用Erik的修改,我也无法得到Darren的工作答案。以下行不断发生错误。

Ctl.Value = Not ClickedControl.Value

下面是一个可能愚蠢的版本,对我来说是一个简单的3切换系统。

Private Sub ToggleButton1_Click()
    If Me.ToggleButton1.Value = False Then Exit Sub
    Me.ToggleButton2.Value = False
    Me.ToggleButton3.Value = False
End Sub
Private Sub ToggleButton2_Click()
    If Me.ToggleButton2.Value = False Then Exit Sub
    Me.ToggleButton1.Value = False
    Me.ToggleButton3.Value = False
End Sub
Private Sub ToggleButton3_Click()
    If Me.ToggleButton3.Value = False Then Exit Sub
    Me.ToggleButton1.Value = False
    Me.ToggleButton2.Value = False
End Sub

唯一的缺点是我可以关闭当前活动的按钮,以便没有活动按钮。我希望如此,如果您尝试关闭活动的,它会保持活动状态。所以一个按钮总是打开。

答案 3 :(得分:0)

在用户表单中:

Dim Buttons() As New Class1

Private Sub UserForm_Initialize()
    Dim ToggleCount     As Integer
    Dim Ctl             As Control

    For Each Ctl In UserForm1.Controls
        If TypeName(Ctl) = "ToggleButton" Then
            ToggleCount = ToggleCount + 1
            ReDim Preserve Buttons(1 To ToggleCount)
            Set Buttons(ToggleCount).ToggleGroup = Ctl
            Buttons(ToggleCount).CtlName = Ctl.Name
        End If
    Next Ctl

End Sub

在课程模块中:

Public WithEvents ToggleGroup As ToggleButton
Public CtlName As String

Private Sub ToggleGroup_click()

' code

End Sub

Private Sub ToggleGroup_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Dim Ctl             As Control

    For Each Ctl In UserForm1.Controls
        If TypeName(Ctl) = "ToggleButton" Then
            If Not Ctl.Name = CtlName Then
                Ctl.Value = False
            End If
        End If
    Next Ctl
End Sub

答案 4 :(得分:0)

'在用户表单中试试这个。

Private Sub ToggleButton1_Click()
    If Me.ToggleButton1.Value = True Then
       Me.ToggleButton2.Value = Not (Me.ToggleButton1.Value)
    Else
       Me.ToggleButton1.Value = Not (Me.ToggleButton2.Value)
End Sub

Private Sub ToggleButton2_Click()
    If Me.ToggleButton2.Value = True Then
       Me.ToggleButton1.Value = Not (Me.ToggleButton2.Value)
    Else
       Me.ToggleButton2.Value = Not (Me.ToggleButton1.Value)
    End If
End Sub

'对我来说完美无缺。 '干杯!