How do I make a Toggle Button work?

andy-s

Board Regular
Joined
Mar 22, 2002
Messages
51
Hi,

I've two macros that I want to link to a toggle button.

My idea is that the button should work like a power switch knop; macro nr 1 should start if I push the button (true=on), and macro nr 2 should begin as soon as I hit the button for the second time (false=off).

And, is it possible to display a button text like "On" and "Off"?

..thx..
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi Andy,

Set your button's action macro as RunMe and use this code. State property which you are looking for to toggle action. And Caption property as the appearing text on button. My command button is on Formatting toolbar as id is 22.

Sub RunMe()
With Application.CommandBars("Formatting").Controls(22)
Select Case .Caption
Case "&On"
.Caption = "&Off"
.State = msoButtonDown
'Call Your On Macro, first macro may be
Case "&Off"
.Caption = "&On"
.State = msoButtonUp
'Call Your Off Macro, other macro
End Select
End With
End Sub

regards
 
Upvote 0

Forum statistics

Threads
1,214,897
Messages
6,122,141
Members
449,066
Latest member
Andyg666

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