VBA – Create a toggle button with code

Sometimes, you may want a button to perform different actions. It is quite easy to make one with an On/Off, but when you would like to make a button, performing 4 different actions in VBA, the story goes a little more interesting. So, here is a small gif, which I have built, that sums, divides, subtracts and multiplies numbers and thus performing 4 different actions with one button.

buttonWith4Actions

So, how is the magic done? 🙂 You probably think that there is a global variable, assigned with value, holding the next action? (If you really thought of this, write in the comments below). Actually, the way it is done is a little more trivial – The code changes the caption of the button and based on its caption, a code changing the cells on line 12 is executed.

Here is how the code looks like:

Enjoy! 🙂

About

VBA Developer