VBA & Macro Problems

How to Show or Hide Protected Excel Worksheets with a Checkbox VBA Macro

Huma Ashraf ChHuma Ashraf Ch Nov 4, 2026 869 views

Question details

The user needs a VBA macro to toggle the visibility of specific worksheets in a protected Excel workbook by clicking a checkbox linked to a cell.

How to Show or Hide Protected Excel Worksheets Using a Checkbox VBA Macro
Product
Excel / WPS Spreadsheet
Device & OS
not provided
Scenario
Creating an interactive dashboard where users can check a box to reveal hidden supplementary sheets, requiring the macro to automatically unprotect, toggle visibility, and reprotect the sheets.
Observed behavior
A programmatic solution is required to detect the checkbox state, manage worksheet or workbook protection passwords, and adjust the Visible property of the target worksheets accordingly.
Before you start

Ensure you have the Developer tab enabled to insert Form Controls and access the VBA editor. Note down your exact worksheet protection password, as you will need to embed it in the macro code.

Solution 1Recommended

Use a Linked Checkbox and Worksheet Change Event

Link a Form Control checkbox to a specific cell and use a Worksheet_Change macro to automatically unprotect, toggle visibility, and reprotect sheets.

This method uses a checkbox linked to cell C1. When the checkbox is clicked, the cell value changes, triggering the macro to unprotect sheets 2 through 6, update their visibility, and lock them again.

1
Insert and Link the Checkbox

Go to the Developer tab, click Insert, and select the Checkbox (Form Control). Draw it on your first worksheet. Right-click the checkbox, select Format Control, and set the Cell link to C1.

2
Open the VBA Editor

Press Alt + F11 to open the Visual Basic for Applications editor. In the Project Explorer on the left, double-click the module for your first worksheet (e.g., Sheet1).

3
Add the VBA Code

Paste the following code into the worksheet module. Replace 'your_password' with your actual password: Private Sub ShowSheets(ByVal sheetsVisible As Boolean) Dim i As Long, password As String password = "your_password" For i = 2 To 6 ThisWorkbook.Worksheets(i).Unprotect Password:=password ThisWorkbook.Worksheets(i).Visible = sheetsVisible ThisWorkbook.Worksheets(i).Protect Password:=password Next i End Sub Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$C$1" Then Application.ScreenUpdating = False ShowSheets (Target.Value = "") Application.ScreenUpdating = True End If End Sub

4
Test the Checkbox

Close the VBA editor. Check and uncheck the box on your worksheet to verify that sheets 2 through 6 appear and disappear as expected.

Use a Linked Checkbox and Worksheet Change Event
Workbook vs Worksheet Protection: If your workbook structure is protected, you must also unprotect the workbook itself before changing worksheet visibility. Protecting a worksheet alone does not always prevent changes to its Visible property.
Advanced Macros in WPS Spreadsheet

Create Powerful VBA Macros with WPS Spreadsheet

WPS Office fully supports VBA (Visual Basic for Applications) and Excel macros, allowing you to easily build interactive dashboards, insert checkboxes, and automate sheet protection tasks with code.

  1. 1. Enable Developer Tools: Open WPS Spreadsheet, go to the Developer tab, and click 'Visual Basic' to access the VBA programming environment.
  2. 2. Insert Form Controls: Under the Developer tab, select 'Insert' to easily add Checkboxes, Option Buttons, and Combo Boxes to your dashboard.
  3. 3. Write and Execute Code: Use the VBA editor to paste or write your automation scripts and safely save your file in macro-enabled (.xlsm) format.
Fully compatible with Microsoft Excel (.xlsm, .xls) macro-enabled files.Built-in VBA editor for creating, debugging, and running complex macros seamlessly.Includes all essential Form Controls, like checkboxes and buttons, for interactive sheets.Free and lightweight alternative to Microsoft Office with a familiar user interface.
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get a "Run-time error '1004'" when trying to hide a sheet via VBA?

This usually happens if the workbook's overall structure is protected. You must unprotect the workbook using `ThisWorkbook.Unprotect "password"` before changing a worksheet's `Visible` property, even if the individual sheets themselves are unprotected.

Can I hide the TRUE/FALSE text in the cell linked to the checkbox?

Yes, you can easily hide the value by changing the font color of the linked cell (e.g., C1) to match the cell's background color (usually white). Alternatively, you can place the linked cell in a hidden column or row.

Does changing the `Visible` property always require unprotecting the worksheet?

Not necessarily. Protecting a worksheet's contents usually restricts editing cells and objects, but it doesn't always lock its visibility. However, if the workbook structure is protected, or if your macro modifies locked cells as part of its process, unprotecting via VBA is required.