Click Twice on a Macro Button

ricardo9316

New Member
Joined
Apr 17, 2014
Messages
22
Is it possible to have a button on a worksheet do different things on its first click to a second click?

For example, I currently have a button that unhides tabs in a worksheet and I would like the tabs to be hidden again if clicked twice, so like a toggle button? My code so far is as follows:


Sub UnhideSheets()


Sheets("Monique").Visible = True

End Sub



Please can anyone help, or would I need a second button to hide the rows again?

Many thanks for taking the time to read my post and any help/guidance would be most appreciated.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Maybe this?

Code:
Sub UnhideSheets()

If Sheets("Monique").Visible = True Then
    Sheets("Monique").Visible = False
Else
    Sheets("Monique").Visible = True
End If

End Sub
 
Upvote 0
A bit more succinct:

Sheets("Monique").Visible = Not Sheets("Monique"").Visible

If you want to get a little fancier, you can even change the caption on the button to say "Hide" or "Unhide"
 
Upvote 0
@Eric W
''If you want to get a little fancier, you can even change the caption on the button to say "Hide" or "Unhide" '' . Can you show me how to do that?

Thanks
 
Upvote 0
What type of button are you using?
 
Upvote 0
What type of button are you using?
A form control one that when its click unhide some rows and the second click hide the same rows. I want that caption of the button to change also.
 
Upvote 0
How about
VBA Code:
Sub Vladf()
   With ActiveSheet.Shapes(Application.Caller).TextFrame.Characters
      If .Caption = "Hide" Then
         'do something
         .Caption = "Unhide"
      Else
         'do something else
         .Caption = "Hide"
      End If
   End With
End Sub
 
Upvote 0
How about
VBA Code:
Sub Vladf()
   With ActiveSheet.Shapes(Application.Caller).TextFrame.Characters
      If .Caption = "Hide" Then
         'do something
         .Caption = "Unhide"
      Else
         'do something else
         .Caption = "Hide"
      End If
   End With
End Sub
Hi,
I adjusted your code with my code and it worked. thank you very much
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,213,552
Messages
6,114,278
Members
448,559
Latest member
MrPJ_Harper

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