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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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,849
Messages
6,121,922
Members
449,056
Latest member
denissimo

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