{"id":12125,"date":"2012-02-09T18:01:44","date_gmt":"2012-02-09T18:01:44","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/?p=1316"},"modified":"2012-02-09T18:01:44","modified_gmt":"2012-02-09T18:01:44","slug":"toggle-button-in-excel","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2012\/02\/toggle-button-in-excel\/","title":{"rendered":"Excel Macro Tutorial : Toggle Button in Excel"},"content":{"rendered":"
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.
\n<\/p>\n
It’s very easy to add Toggle Button in your workbook. Follow the below steps to add toggle button:
\n<\/p>\n
Go to <\/p>\n <\/a><\/p>\n Click on the Toggle Button Icon from the Control as shown below: Toggle Button<\/p><\/div> Now Double Click on that Button Toggle Button<\/p><\/div> Here you can write your Code. Toggle Button VBE Code<\/p><\/div> I am going to explain you by giving two examples, how we can use this button in real time applications. 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. <\/p>\n i) Add the Code of sorting in Ascending and Descending in a Regular Module in VBE of your Workbook<\/p>\n <\/p>\n <\/a><\/p>\n ii) Paste the below code in the VBE of the Worksheet where you have Put your button:<\/p>\n <\/p>\n <\/a><\/p>\n <\/p>\nDeveloper’s Tab<\/h3>\n
Step 2. <\/h3>\n
\n<\/p>\n<\/a>
\n<\/p>\nStep 3. <\/h3>\n
\n
\n<\/a>
\n<\/p>\nStep 4. <\/h3>\n
\n
\n<\/a>
\n<\/p>\nExample: <\/h3>\n
\n<\/p>\n1. Hiding\/un-hiding Row or Columns by Toggle Button: <\/h3>\n
\n<\/p>\n\nPrivate Sub ToggleButton1_Click()\n'For hiding and un-hiding Columns\n Columns(\"C:F\").Hidden = Not Columns(\"C:F\").Hidden\n'For hiding and un-hiding Rows\n Rows(\"1:2\").Hidden = Not Rows(\"1:2\").Hidden\nEnd Sub\n<\/code><\/pre>\n
2. Sorting in Ascending and Descending Order by Using Toggle Button: <\/h3>\n
To get the Code for Sorting in Ascending or Descending<\/h3>\n
\n\nPrivate Sub ToggleButton1_Click()\n\t\n\tIf ToggleButton1.Value = True Then\n\t\tCall Sort_Ascending\n\tElse\n\t\tCall Sort_Descending\n\tEnd If\n\t\nEnd Sub\n\n<\/code><\/pre>\n
To Know About Spin Button in Excel Click Here<\/h3>\n