Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Make a simple button behave like a toggle button?
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Mar 2019
    Location
    Republic of Mauritius
    Posts
    77
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Make a simple button behave like a toggle button?

    Hi, everybody

    Suppose I have a simple button (not a toggle button).

    I want it to run the macro "Macro1" when I click on it the first time, and run "Macro2" when I click it a second time.

    If I click it a third time, it will run "Macro1" again...

    I want something simple, because I will later need to translate the macros into VB .NET.

    How can this be done?

    Thanks.

    Leon

  2. #2
    Board Regular NdNoviceHlp's Avatar
    Join Date
    Nov 2002
    Location
    Manitoba Canada
    Posts
    2,277
    Post Thanks / Like
    Mentioned
    6 Post(s)
    Tagged
    2 Thread(s)

    Default Re: How can we make a simple button behave like a toggle button?

    Just change the colour of the button ie.if the button is green, run only macro1 and turn it red. If the button is red, run only macro2 and change it to green. HTH. Dave

  3. #3
    Board Regular Tim_Excel_'s Avatar
    Join Date
    Jul 2016
    Posts
    512
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    4 Thread(s)

    Default Re: How can we make a simple button behave like a toggle button?

    Look into static variables. They hold their value even when the macro has finished running. You could potentially set a static boolean variable and then determine which macro to run with something along the lines of

    Code:
    If togglevar = True Then
        Macro1
    Else
        Macro2
    End if
    Always make a backup of your data before trying out any code

    Before you ask why my code is not working: have you checked if the references are correct? Are you referring to the correct Workbook/-sheet?

    Use [CODE] tags when pasting code in your posts

  4. #4
    Board Regular
    Join Date
    Mar 2019
    Location
    Republic of Mauritius
    Posts
    77
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: How can we make a simple button behave like a toggle button?

    Hi,

    Do not bother, unless there is a better solution. I found the solution:

    Sub Toggle()
    Code:
    Public Flag1 As String
    Code:
    If Flag1 = "A" Or Flag1 = "" Then
    Call Macro1
    Flag1 = "B"
    
    
    Thanks
    Leon
    
    
    
    
    ElseIf Flag1 = "B" Then
    Call Macro2
    Flag1 = "A"
    
    
    End If
    
    
    End Sub

  5. #5
    Board Regular
    Join Date
    Mar 2019
    Location
    Republic of Mauritius
    Posts
    77
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: How can we make a simple button behave like a toggle button?

    Hi, Tim_Excel_

    Thanks a lot for your reply.

    Our answers crossed, and they correspond. I will try your idea of Static variable instead of Public.

    Best Regards,
    Leon


    ====

    ADDED:
    Static works fine! Thanks
    Last edited by leonlai; Apr 2nd, 2019 at 08:45 AM.

  6. #6
    MrExcel MVP Rick Rothstein's Avatar
    Join Date
    Apr 2011
    Location
    New Jersey, USA
    Posts
    35,232
    Post Thanks / Like
    Mentioned
    92 Post(s)
    Tagged
    33 Thread(s)

    Default Re: How can we make a simple button behave like a toggle button?

    Another simple idea is to change the caption for the button (make the caption meaningful so the user knows what pressing the button means) and key which macro to run depending on the caption displayed on the button.
    Rick's "mini" blog... http://www.excelfox.com/forum/f22/
    .
    Want to post a small screen shot? See Part B here.

  7. #7
    Board Regular
    Join Date
    Mar 2019
    Location
    Republic of Mauritius
    Posts
    77
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: How can we make a simple button behave like a toggle button?

    Hi, Rick

    That's a great idea!

    My button works correctly, but the caption stays fixed. It would be helpful if it changes also.

    Could you give me an idea how you can accomplish that?

    Thanks
    Leon

  8. #8
    Board Regular
    Join Date
    Mar 2019
    Location
    Republic of Mauritius
    Posts
    77
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: How can we make a simple button behave like a toggle button?

    Hi, NdNoviceHlp

    Thanks for your reply.

    How can you do that?

    Leon

  9. #9
    Board Regular NdNoviceHlp's Avatar
    Join Date
    Nov 2002
    Location
    Manitoba Canada
    Posts
    2,277
    Post Thanks / Like
    Mentioned
    6 Post(s)
    Tagged
    2 Thread(s)

    Default Re: How can we make a simple button behave like a toggle button?

    Rick's would be something like this...
    Code:
    If UserForm1.CommandButton1.Caption = "Macro1" Then
    'code for macro1
    UserForm1.CommandButton1.Caption = "Macro2"
    Else
    'code for macro2
    UserForm1.CommandButton1.Caption = "Macro1"
    End If
    Change colours...
    Code:
    If UserForm1.CommandButton1.BackColor = RGB(255, 0, 0) Then 'red
    'code for macro1
    UserForm1.CommandButton1.BackColor = RGB(0, 128, 64) 'green
    Else
    'code for macro2
    UserForm1.CommandButton1.BackColor = RGB(255, 0, 0)
    End If
    U will need to adjust the userform name and command button name. Also to start, U will need to place some ode in the initialize event. ie either of the following based on what U use...
    Code:
    Private Sub UserForm_Initialize()
    UserForm1.CommandButton1.Caption = "Macro1"
    UserForm1.CommandButton1.BackColor = RGB(255, 0, 0)
    End Sub
    HTH. Dave

  10. #10
    MrExcel MVP Rick Rothstein's Avatar
    Join Date
    Apr 2011
    Location
    New Jersey, USA
    Posts
    35,232
    Post Thanks / Like
    Mentioned
    92 Post(s)
    Tagged
    33 Thread(s)

    Default Re: How can we make a simple button behave like a toggle button?

    Quote Originally Posted by leonlai View Post
    Hi, Rick

    That's a great idea!

    My button works correctly, but the caption stays fixed. It would be helpful if it changes also.

    Could you give me an idea how you can accomplish that?
    What kind of button is it... one found on a UserForm or, if on a worksheet, is it a Forms button or an ActiveX button?
    Rick's "mini" blog... http://www.excelfox.com/forum/f22/
    .
    Want to post a small screen shot? See Part B here.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •