A toggle button in Excel VBA is a control that switches between two states—typically ON and OFF—with each click. It's useful for showing/hiding rows, enabling/disabling calculations, or changing chart views. You can add it via the Developer tab (Form Controls or ActiveX) and write a short VBA macro to react to clicks. The steps below cover both the setup and the code, and they work in Excel 2010 through Microsoft 365.

What You'll Need

  • Excel with the Developer tab visible. If you don't see it, right-click the ribbon and choose Customize the Ribbon, then check Developer.
  • Basic familiarity with the Visual Basic Editor (VBE) — you'll be writing a few lines of code.
  • Macros enabled in your workbook. When you open it later, choose Enable Content if prompted.

Step-by-Step

  1. Open the Developer tab – In Excel, click the Developer tab on the ribbon. If you don't have it, follow the note above to enable it.
  2. Insert a toggle button – In the Controls group, click Insert. You'll see two sections: Form Controls and ActiveX Controls. Choose the Toggle Button icon (it looks like a small button with a checkmark) from either section. The ActiveX version gives you more formatting options, but the Form Control version is simpler. For this guide, we'll use the ActiveX toggle button.
  3. Draw the button – Click anywhere on your worksheet and drag to draw the button. You can resize or move it later.
  4. Rename the button (optional) – Right-click the button and choose Properties. In the Properties window, change the Caption to something like "Show/Hide Details" and the Name to something like tglDetails. Close the Properties window.
  5. Open the VBA editor – Right-click the button and select View Code. This opens the VBA editor with a blank subroutine named after the button's click event (e.g., tglDetails_Click).
  6. Write the toggle code – Inside that subroutine, add code that checks the button's Value property (True = pressed/ON, False = not pressed/OFF) and performs the desired action. For example, to hide or show rows 2 to 10:
    Private Sub tglDetails_Click()
        If tglDetails.Value = True Then
            Rows("2:10").Hidden = False
        Else
            Rows("2:10").Hidden = True
        End If
    End Sub
  7. Test the button – Return to the worksheet (click the Excel icon in the VBA toolbar or press Alt+F11 to switch). Click the button. The rows should hide or show depending on the button's state. The button will appear "pressed" when ON.
  8. Save as macro-enabled – When you save the workbook, choose Excel Macro-Enabled Workbook (*.xlsm) in the Save As dialog, otherwise the code will be lost.

Common Mistakes

  • Forgetting to enable macros – If you open the file and the button does nothing, check the yellow security bar and click Enable Content.
  • Using the wrong control type – A Form Control toggle button uses a different event model (you assign a macro via right-click > Assign Macro). The code above is for ActiveX. If you use a Form Control, the subroutine name won't match automatically.
  • Not setting the button's Value to False initially – If the button starts as True (pressed) when the workbook opens, your code may run unexpectedly. Set Value = False in the Properties window or in the Workbook_Open event.
  • Hardcoding row numbers – If you insert or delete rows, the hardcoded range (e.g., "2:10") may no longer be correct. Consider using named ranges or dynamic references.
  • Using the wrong property – For ActiveX controls, the state is Value (True/False). For Form Controls, it's Value as well, but the macro receives a parameter (e.g., LinkedCell). Mixing them up causes errors.

Tips

  • Use a linked cell – For a Form Control toggle button, you can set a Linked Cell in the Format Control dialog. That cell will contain TRUE/FALSE, which you can reference in formulas.
  • Improve readability – Change the button's BackColor or Caption dynamically in the code to reflect the state. For example:
    If tglDetails.Value = True Then
        tglDetails.Caption = "Hide Details"
    Else
        tglDetails.Caption = "Show Details"
    End If
  • Group multiple toggles – If you have several toggle buttons that should act as radio buttons (only one ON at a time), you'll need to loop through them and set the others to False.
  • Use Option Explicit – At the top of your module, type Option Explicit to force variable declaration. This prevents typos in variable names.
  • Test with different zoom levels – ActiveX buttons can sometimes appear distorted when zoomed. If that happens, adjust the button's Placement property to 1 (move but don't size with cells) in the Properties window.

Troubleshooting

Button does nothing when clicked – First, check that macros are enabled. Then, right-click the button and select Properties to verify the Name matches the subroutine name in the code. If you're using a Form Control, ensure you've assigned the macro via Assign Macro.

Button text doesn't change – If you change the Caption in the Properties window but it reverts, your code might be overwriting it. Look for any line that sets .Caption in the code and adjust it.

Toggle works but the state is inverted – That happens when you check Value = False when you meant True. Review the condition logic. Also check if the button's Value is being reset by something else (like a worksheet change event).

Button disappears or is behind other shapes – Right-click the button, choose Properties, and set Placement to 1 (move but don't size with cells) or 2 (move and size with cells). Also check the Order via the Selection Pane (Home > Editing > Find & Select > Selection Pane).

Error "Compile error: Sub or Function not defined" – This usually means the subroutine name in the code doesn't match the control's name. In the VBA editor, find the control's name in the Properties window (e.g., tglDetails) and ensure the subroutine is Private Sub tglDetails_Click().

If all else fails – Delete the button and insert a new one. Recreate the code from scratch. Also try closing and reopening Excel to clear any stuck references.

Frequently Asked Questions

Can I use a toggle button to run a macro that doesn't depend on its state?

Yes. The click event fires every time the button is clicked, regardless of the state. You can just place any code inside the subroutine without checking Value.

What's the difference between a Form Control and an ActiveX toggle button?

Form Controls are simpler, lighter, and have fewer properties. ActiveX controls offer more formatting options (like back color, font) and have a richer event model. For basic toggling, either works.

How do I make the toggle button default to OFF when the workbook opens?

In the Properties window (for ActiveX), set Value to False. Alternatively, in the Workbook_Open event, add Sheet1.tglDetails.Value = False (replace Sheet1 with your sheet name).

Can I use a toggle button to control a chart's visibility?

Yes. In the click event, set ChartObject.Visible to True or False based on the button's state. For example: Sheet1.ChartObjects("Chart 1").Visible = tglChart.Value.

Why does my toggle button not work after I protect the worksheet?

Sheet protection blocks editing of controls unless you allow it. In the Protect Sheet dialog, check the box for Edit objects. Alternatively, unprotect the sheet in the click event before making changes, then re-protect it.

Can I have multiple toggle buttons that toggle each other?

Yes. In the click event of one button, you can set another button's Value to False. This creates a radio-button behavior.