VBA Excel切换按钮“锁定”

时间:2016-07-25 14:01:02

标签: excel vba excel-vba vbscript

我在Excel中的VBA程序中有以下代码对应于三个ToggleButtons。单击一个按钮时,它应该保持“按下”状态,其他两个按钮应该“释放”。但是,根据我的代码,我必须单击一个按钮TWICE,一次释放其他按钮,另一个按钮保持原始按钮。

但是,如果我为每个Subs添加“ToggleButtonX.Value = True”,当单击一个按钮时,即使单击另一个按钮也无法释放。如何配置这个,以便当单击一个按钮时,该按钮保持按下状态,其他按钮被释放?

编辑:我想保持TOGGLEBUTTONS。

Private Sub ToggleButton1_Click()
    ToggleButton2.Value = False
    ToggleButton3.Value = False
End Sub

Private Sub ToggleButton2_Click()
    ToggleButton1.Value = False
    ToggleButton3.Value = False
End Sub

Private Sub ToggleButton3_Click()
    ToggleButton1.Value = False
    ToggleButton2.Value = False
End Sub

3 个答案:

答案 0 :(得分:3)

通过调用ToggleButtonx.Value = False,您正在模拟对该按钮的单击,因此它将运行自己的代码,并将您刚刚单击的按钮的值设置为false。

改为使用MouseDown事件:

Private Sub ToggleButton1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    ToggleButton2.Value = False
    ToggleButton3.Value = False
End Sub
Private Sub ToggleButton2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    ToggleButton1.Value = False
    ToggleButton3.Value = False
End Sub
Private Sub ToggleButton3_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    ToggleButton1.Value = False
    ToggleButton2.Value = False
End Sub

答案 1 :(得分:3)

除了Oliver的答案,它可能更容易和更清晰地实现,还有另一种方法可以使用逻辑,只在单击一个按钮时更改其他两个按钮的值。其他按钮Click事件的代码将会触发,但变量sButton将确定代码只会触发一个用户按下 phyiscally 按钮的按钮。

另请注意Not Me.ToggleButton1.Value的使用。这将确保按钮2和3与按钮1 相反,每次单击。您编写代码的方式是,无论点击按钮是False还是True,它都会将其他按钮还原为False

Option Explicit

Public sButton As String

Private Sub ToggleButton1_Click()

ButtonLoad 1

End Sub

Private Sub ToggleButton2_Click()

ButtonLoad 2

End Sub

Private Sub ToggleButton3_Click()

ButtonLoad 3

End Sub

Sub ButtonLoad(iButton As Integer)

Select Case iButton
    Case 1
        If sButton = "" Then
            sButton = "1" 'set so that other buttons don't trigger
            Me.ToggleButton2.Value = Not Me.ToggleButton1.Value
            Me.ToggleButton3.Value = Not Me.ToggleButton1.Value
            sButton = "" 'reset for next button click
        End If
    Case 2
        If sButton = "" Then
            sButton = "2"
            Me.ToggleButton1.Value = Not Me.ToggleButton2.Value
            Me.ToggleButton3.Value = Not Me.ToggleButton2.Value
            sButton = ""
        End If
    Case 3
        If sButton = "" Then
            sButton = "3"
            Me.ToggleButton2.Value = Not Me.ToggleButton3.Value
            Me.ToggleButton1.Value = Not Me.ToggleButton3.Value
            sButton = ""
        End If
End Select

End Sub

答案 2 :(得分:1)

使用选项按钮可以轻松完成(如果这与其他实现一起使用)

Private Sub OptionButton1_Click()
    OptionButton1.Value = True
    OptionButton2.Value = False
    OptionButton3.Value = False
End Sub

Private Sub OptionButton2_Click()
    OptionButton1.Value = False
    OptionButton2.Value = True
    OptionButton3.Value = False
End Sub

Private Sub OptionButton3_Click()
    OptionButton1.Value = False
    OptionButton2.Value = False
    OptionButton3.Value = True
End Sub