VBA code to toggle "XYZ" macro on and off

jkalpus

New Member
Joined
Apr 17, 2004
Messages
44
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!
 

Joe Was

MrExcel MVP
Joined
Feb 19, 2002
Messages
7,539
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:

AtWork

Board Regular
Joined
Dec 27, 2006
Messages
128
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.
 

Forum statistics

Threads
1,085,841
Messages
5,386,278
Members
401,991
Latest member
jimmywhitetrix

Some videos you may like

This Week's Hot Topics

Top