VBA & Macro Problems

Fix Excel ActiveX Toggle Buttons Not Hiding Rows

Phi Hung VoPhi Hung Vo Nov 11, 2026 868 views

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.

How to Fix Excel ActiveX Toggle Buttons That Fail to Hide Different Rows
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.
Before you start

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.

Solution 1Recommended

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.

1
Enable Design Mode

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.

2
Open the Worksheet Module

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.

3
Create a New Click Event

Ensure the procedure block is named specifically for the second button, for example: Private Sub ToggleButton2_Click().

4
Update Object References

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.

5
Assign the Target Rows

Update the row range that this specific button should hide. For example, use the code: Rows("80:90").Hidden = ToggleButton2.Value.

6
Declare Variables

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.

7
Exit Design Mode and Test

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.

Update the VBA Code References for the Second Toggle Button
Testing Your Controls: ActiveX buttons will only trigger their click events when Design Mode is turned off. If the button appears selected but nothing happens, check that macros are not being blocked by Excel's security alerts.
Seamless VBA Integration

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. 1. Open Your Macro Workbook: Download and install WPS Office, then open your existing .xlsm file in WPS Spreadsheet.
  2. 2. Access the Developer Tools: Navigate to the Developer tab on the ribbon to access the VBA Editor and Insert ActiveX controls.
  3. 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. 4. Run the Macros: Exit Design Mode to immediately test your interactive buttons and macros directly within the spreadsheet environment.
Fully compatible with Microsoft Excel .xlsm and .xlsb macro-enabled formats.Built-in Developer tab and VBA editor for creating and modifying ActiveX toggle buttons.Lightweight application that runs complex macros and automated scripts with high performance.Free to download with a highly familiar spreadsheet interface.
microsoft office alternative - wps office

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.