VBA & Macro Problems

Hide Excel Sheets Except Control and a Selected Sheet with VBA

Ayan MasoodAyan Masood Nov 27, 2026 869 views

Question details

Keep the Control worksheet and the worksheet named in Control!A1 visible while making all other worksheets VeryHidden.

Hide Excel Sheets Except Control and a Selected Sheet with VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A workbook uses the value in Control!A1 to determine which worksheet should remain visible alongside the Control sheet.
Observed behavior
The desired result is for the selected worksheet and Control sheet to be visible and all other worksheets to be VeryHidden.
Before you start

Confirm that the workbook contains a worksheet named Control and that A1 contains or returns an existing worksheet name. Unprotect the workbook structure through Review > Protect Workbook if it is protected, and save the file as an Excel Macro-Enabled Workbook (.xlsm).

Solution 1Recommended

Use workbook events to update worksheet visibility

Use this VBA code to update visibility when the workbook opens, when A1 is edited, or when the Control sheet recalculates.

The code validates the name in A1 before changing visibility. It makes the Control and selected worksheets visible first, then sets other worksheets to xlSheetVeryHidden. A worksheet name constant must be a quoted string, such as "Control".

The calculation event also supports an A1 formula whose result changes after recalculation. The code applies to worksheets, not chart sheets.

1
Open the workbook code module

In desktop Excel, press Alt+F11 to open the VBA editor. In Project Explorer, find your workbook and double-click ThisWorkbook.

2
Add the event code

Paste the following code into ThisWorkbook. If the module already contains any of these event procedures, merge the calls into the existing procedures instead of creating duplicates: Private Sub Workbook_Open() UpdateVisibleSheets End Sub Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) If Sh.Name = "Control" Then If Not Intersect(Target, Sh.Range("A1")) Is Nothing Then UpdateVisibleSheets End If End If End Sub Private Sub Workbook_SheetCalculate(ByVal Sh As Object) If Sh.Name = "Control" Then UpdateVisibleSheets End Sub Private Sub UpdateVisibleSheets() Const CONTROL_NAME As String = "Control" Dim ctl As Worksheet, ws As Worksheet, selectedSheet As Worksheet Dim selectedName As String Dim eventsWereEnabled As Boolean, eventsChanged As Boolean On Error GoTo Failed Set ctl = Me.Worksheets(CONTROL_NAME) If IsError(ctl.Range("A1").Value2) Then Exit Sub selectedName = Trim$(CStr(ctl.Range("A1").Value2)) For Each ws In Me.Worksheets If StrComp(ws.Name, selectedName, vbTextCompare) = 0 Then Set selectedSheet = ws Exit For End If Next ws If selectedSheet Is Nothing Then Exit Sub eventsWereEnabled = Application.EnableEvents Application.EnableEvents = False eventsChanged = True ctl.Visible = xlSheetVisible selectedSheet.Visible = xlSheetVisible For Each ws In Me.Worksheets If ws.Name <> ctl.Name And ws.Name <> selectedSheet.Name Then ws.Visible = xlSheetVeryHidden End If Next ws CleanUp: If eventsChanged Then Application.EnableEvents = eventsWereEnabled Exit Sub Failed: MsgBox "Could not update sheet visibility: " & Err.Description, vbExclamation Resume CleanUp End Sub

3
Save and test the workbook

Save the .xlsm file, close it, and reopen it with macros enabled. Enter an existing worksheet name in Control!A1, or recalculate the workbook if A1 contains a formula, and check the visible sheet tabs.

Use workbook events to update worksheet visibility
VeryHidden is not a security feature: VeryHidden worksheets do not appear in Excel's normal Unhide dialog, but someone with VBA access can change their visibility. If A1 is blank, contains an error, or names no worksheet, the code leaves visibility unchanged.
Free Microsoft Office alternative

Work with Excel files in WPS Spreadsheets

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for Microsoft Office formats, making it easy to move everyday workbook tasks to WPS. The Excel VBA event solution above remains specific to Excel; macro support in WPS can vary by version and platform.

Use familiar worksheet tabs and spreadsheet controlsOpen and edit Microsoft Excel formats such as .xlsxMove everyday spreadsheet work to a lightweight Office suiteUse a free alternative for common document and spreadsheet tasks
microsoft office alternative - wps office

Frequently Asked Questions

What happens if Control!A1 contains an invalid sheet name?

The code checks the name against the workbook's worksheets and exits without changing visibility if there is no match. Enter an existing worksheet name in Control!A1 and try again.

Will the code run if A1 contains a formula?

Yes. The Workbook_SheetCalculate event updates visibility when the Control sheet recalculates. Workbook_SheetChange alone would not detect a formula result changing through recalculation.

How can I make a VeryHidden worksheet visible again?

In desktop Excel, press Alt+F11, select the worksheet in Project Explorer, and change its Visible property to -1 - xlSheetVisible in the Properties window. VeryHidden sheets are not listed in the normal Unhide dialog.

Why does Excel report an error when changing sheet visibility?

A protected workbook structure can prevent visibility changes. Check Review > Protect Workbook and unprotect the structure if needed, then verify that the workbook contains a sheet named Control.