Fix Excel ActiveX Toggle Buttons Not Hiding Rows
Question details
The user needs to make a second ActiveX toggle button hide and unhide specific rows independently, which is currently failing due to incorrect VBA object references.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Creating multiple ActiveX toggle buttons on an Excel worksheet to hide or show different sets of rows based on the button's clicked state.
- Observed behavior
- The first button successfully hides and unhides its assigned rows, but the second button does not work because its VBA procedure incorrectly references the first toggle button instead of itself.
Ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that your Trust Center security settings allow ActiveX controls and macros to run.
Update the VBA Code References for the Second Toggle Button
Fix the control references in the worksheet module so the second toggle button operates completely independently from the first one.
When copying VBA code from one ActiveX control to another, the object names and event handlers must be explicitly updated. If the second button's code still references 'ToggleButton1', it will modify the state of the first button or fail entirely instead of evaluating its own click event.
Go to the Developer tab on the Excel ribbon and click 'Design Mode' so that it is highlighted. This prevents the buttons from executing macros while you are editing them.
Right-click the second toggle button on your spreadsheet and select 'View Code'. This will open the Visual Basic for Applications (VBA) editor directly to the code module for that specific worksheet.
Ensure the procedure block is named specifically for the second button, for example: Private Sub ToggleButton2_Click().
Inside the click event, change all references from 'ToggleButton1' to 'ToggleButton2'. You will need to test ToggleButton2.Value and update ToggleButton2.Caption if you are changing the button text dynamically.
Update the row range that this specific button should hide. For example, use the code: Rows("80:90").Hidden = ToggleButton2.Value.
If you are using strict variable declaration, ensure you declare any required variables at the top of the event, such as: Dim part As Variant.
Close the VBA editor, return to the Excel window, and click 'Design Mode' on the Developer tab again to turn it off. Click the second toggle button to verify it now hides the correct rows.

Easily Manage Macros and VBA with WPS Office
WPS Spreadsheet offers comprehensive support for VBA macros, ActiveX controls, and developer tools, allowing you to create and fix complex interactive worksheets effortlessly.
- 1. Open Your Macro Workbook: Download and install WPS Office, then open your existing .xlsm file in WPS Spreadsheet.
- 2. Access the Developer Tools: Navigate to the Developer tab on the ribbon to access the VBA Editor and Insert ActiveX controls.
- 3. Edit the Toggle Button Code: Turn on Design Mode, right-click your toggle buttons, select 'View Code', and apply the correct VBA scripts to ensure each button targets the right rows.
- 4. Run the Macros: Exit Design Mode to immediately test your interactive buttons and macros directly within the spreadsheet environment.

Frequently Asked Questions
Why is my ActiveX toggle button unclickable or showing resize handles?
You are likely still in Design Mode. When Design Mode is active, ActiveX controls are in edit mode and cannot be executed. Go to the Developer tab and click 'Design Mode' to turn it off.
Can I use standard Form Controls instead of ActiveX toggle buttons?
Yes, you can use Form Control checkboxes or regular buttons assigned to a standard macro. However, ActiveX toggle buttons are often preferred for hiding rows because they have a built-in 'pressed/unpressed' state via the Value property, making the VBA logic simpler.
Why do I get a 'Variable not defined' error in my VBA code?
If your VBA module has 'Option Explicit' declared at the top, Excel requires you to explicitly declare all variables. You must add a declaration using the 'Dim' statement (e.g., Dim part As Variant) before using the variable in your ToggleButton code.




