Some times, we require a single control or Button which can perform different action on first click and some other Action on the second click. This can be achieved by adding Toggle Button in your Excel Workbook.
How to Add Toggle Button in Excel Workbook:
It’s very easy to add Toggle Button in your workbook. Follow the below steps to add toggle button:
Click on the Toggle Button Icon from the Control as shown below:
Now Double Click on that Button
Here you can write your Code.
I am going to explain you by giving two examples, how we can use this button in real time applications.
1. Hiding/un-hiding Row or Columns by Toggle Button:
In this case, Clicking on the same button, each time different action will be performed, it means, once it will hide it and next time it will un-hide it.
Private Sub ToggleButton1_Click() 'For hiding and un-hiding Columns Columns("C:F").Hidden = Not Columns("C:F").Hidden 'For hiding and un-hiding Rows Rows("1:2").Hidden = Not Rows("1:2").Hidden End Sub
2. Sorting in Ascending and Descending Order by Using Toggle Button:
i) Add the Code of sorting in Ascending and Descending in a Regular Module in VBE of your Workbook
To get the Code for Sorting in Ascending or Descending
ii) Paste the below code in the VBE of the Worksheet where you have Put your button:
Private Sub ToggleButton1_Click() If ToggleButton1.Value = True Then Call Sort_Ascending Else Call Sort_Descending End If End Sub
To Know About Spin Button in Excel Click Here
To Check out more Excel Macro Tutorials, visit Excel Macro Tutorial