VBA code to toggle "XYZ" macro on and off

jkalpus

New Member
Joined
Apr 17, 2004
Messages
46
Greetings. I have a simple macro assigned to a button on a toolbar. Clicking the button runs the macro which displays two custom toolbars. I want to have the button TOGGLE the macro on and off (thereby turning the two custom toolbars ON and OFF). What's the VBA code?
Thanks!
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Add a toggle switch macro to load a switch to be used in your code!

Public myTog As Boolean

Sub myCodeOnOff()

If myTog = True Then
myTog = False
Else

myTog = True
End If
Exit sub


Then in the code you want to control use:

Sub YourSub()

If myTog <> True Then Exit Sub

'TheRestOfYourEventCodeHere!
Exit Sub

Together these two will allow your code to be toggled on or off with a macro working as a flip-flop switch.

I don't think your post is worded correctly though?
You do not want to turn the macro on or off, you want to run one macro to load a toolbar and another to remove it?
<!-- / message --><!-- sig -->
 
Last edited:
Upvote 0
not sure the specific code but what about this as a theory:


create a new variable at the very start of your macro and set it to a value of 0.


put a giant If statement around your current 'one way' macro


now you can do something at the start of the button click macro like


If new_variable = 0

then

run your current macro to show your custom toolbars,


after that set your new_variable = 1

then at the end of that have the flip side of your 'turn on' macro to turn those toolbars off if that new_variable = 1, and make sure at the end of the 'turn off' phase you set that new_variable back to 0.
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,387
Members
448,956
Latest member
JPav

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