Click a macro button twice

pincivma

Board Regular
Joined
Dec 12, 2004
Messages
206
Hi again
I'm pretty sure that this is doable but I'm having a hard time with the macro. I'll make it simple for you Excel gurus out there. Say I have only one button that executes a macro. Say the macro puts a one in Cell A1. Then when I click on the same macro button, it puts in a zero in cell A1. My macro is very complex but if someone out there can give me a hint, I can probably figure it out myself, MAYBE.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Pincivma,
Try this in your button click event:
Code:
If Sheet1.Cells(1,1).value=”1” then
    Sheet1.Cells(1,1).value=0” 
Else
    Sheet1.Cells(1,1).value=”1”
End if
Replace the ‘sheet1’ with the name of your sheet.
Computerman
 
Upvote 0
Hi pincivma,
I guess you put 0 or 1 in cell A1 just as an example of macro with an alternating behavior between calls

as a starting point

Code:
Sub MyButton()
Static a As Long
a = a + 1
If a Mod 2 = 0 Then
    Cells(1, 1).Value = 0
    'or other code
Else
    Cells(1, 1).Value = 1
    'or other code
End If
End Sub

Hope this helps
 
Upvote 0
Hi pincivma,
I guess you put 0 or 1 in cell A1 just as an example of macro with an alternating behavior between calls

as a starting point

Code:
Sub MyButton()
Static a As Long
a = a + 1
If a Mod 2 = 0 Then
    Cells(1, 1).Value = 0
    'or other code
Else
    Cells(1, 1).Value = 1
    'or other code
End If
End Sub

Hope this helps

Thanks. I will give it a try and see how it works out in my complicated macro
 
Upvote 0
Code:
Sub Maybe_A()
    [A1] = IIf([A1] = 1, 0, 1)
End Sub
Code:
Sub Maybe_B()    'Thanks To Rick Rothstein
    [A1] = 1 - [A1]
End Sub
 
Upvote 0
I like to alter the caption of a button to toggle (or multiple) its actions.
That way the user knows what it will do and its state will be preserved between runnings of Excel.
With a Form's Button the code would look like

Code:
Sub Button1_Click()
    With ActiveSheet.Shapes(Application.Caller).TextFrame.Characters
        Select Case .Text
            Case "One Thing"
                Rem do one thing
                .Text = "Thing Two"
            Case "Thing Two"
                Rem do thing two
                .Text = "Third"
            Case Else
                Rem do a third thing
                .Text = "One Thing"
        End Select
    End With
End Sub
 
Last edited:
Upvote 0
I like to alter the caption of a button to toggle (or multiple) its actions.
That way the user knows what it will do and its state will be preserved between runnings of Excel.
With a Form's Button the code would look like

Code:
Sub Button1_Click()
    With ActiveSheet.Shapes(Application.Caller).TextFrame.Characters
        Select Case .Text
            Case "One Thing"
                Rem do one thing
                .Text = "Thing Two"
            Case "Thing Two"
                Rem do thing two
                .Text = "Third"
            Case Else
                Rem do a third thing
                .Text = "One Thing"
        End Select
    End With
End Sub

Hi Mike

I tested out your button macro and it worked great. I tried only 2 cases instead of your 3 but I could not get the macro to work properly. Can you resend your macro with only 2 cases? I want to see where I went wrong. Thanks.
 
Upvote 0
The key is to list one specific case and one Case Else so that any oddityie are swept up in the Case Else
Code:
Sub Button1_Click()
    With ActiveSheet.Shapes(Application.Caller).TextFrame.Characters
        Select Case .Text
            Case "One Thing"
                Rem do one thing
                .Text = "Thing Two"
            Case Else
                Rem do the second thing
                .Text = "One Thing"
        End Select
    End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top