click and unclick on button

User avatar
sal21
PlatinumLounger
Posts: 4347
Joined: 26 Apr 2010, 17:36

click and unclick on button

Post by sal21 »

peraphs just post, but not found...

i remember only a static or boolean condition..

i need to click to go my sub and if i reclick goto mysub1...

User avatar
HansV
Administrator
Posts: 78407
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: click and unclick on button

Post by HansV »

Something like this:

Code: Select all

Private Sub MyButton_Click()
    Static f As Boolean
    f = Not f
    If f Then
        Call MySub
    Else
        Call MySub1
    End If
End Sub
Alternatively, use the caption of the button:

Code: Select all

Private Sub MyButton_Click()
    If MyButton.Caption = "This" Then
        Call MySub
        MyButton.Caption = "That"
    Else
        Call MySub1
        MyButton.Caption = "This"
    End If
End Sub
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4347
Joined: 26 Apr 2010, 17:36

Re: click and unclick on button

Post by sal21 »

HansV wrote:
06 Sep 2021, 07:40
Something like this:

Code: Select all

Private Sub MyButton_Click()
    Static f As Boolean
    f = Not f
    If f Then
        Call MySub
    Else
        Call MySub1
    End If
End Sub
Alternatively, use the caption of the button:

Code: Select all

Private Sub MyButton_Click()
    If MyButton.Caption = "This" Then
        Call MySub
        MyButton.Caption = "That"
    Else
        Call MySub1
        MyButton.Caption = "This"
    End If
End Sub
NICE!

User avatar
SpeakEasy
4StarLounger
Posts: 544
Joined: 27 Jun 2021, 10:46

Re: click and unclick on button

Post by SpeakEasy »

>click and unclick on button

Sounds to me like you are trying to implement t your own version of a togglebutton - which already exists for userforms, and they already have the equivalent of a static variable representing current status. So you can go with

Code: Select all

Private Sub ToggleButton1_Click()
    With ToggleButton1
        If .Value Then
            .Caption = "Clicked"
        Else
            .Caption = "Unclicked"
        End If
    End With
End Sub