VBA & Macro Problems

Unhide an Excel Worksheet When a Hyperlink Is Selected with VBA

Jason MillerJason Miller Nov 25, 2026 871 views

Question details

Open a hidden worksheet from a selection on the main sheet and hide it again when returning.

Unhide an Excel Worksheet When a Hyperlink Is Selected with VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A workbook uses links or chart labels on a main sheet to navigate to hidden worksheets.
Observed behavior
A VBA selection handler can produce a type mismatch when the selected value is not a valid worksheet name.
Before you start

Use desktop Excel, save the workbook as an .xlsm file, and enable macros only if you trust the workbook. Make sure workbook structure protection is off under Review > Protect Workbook so VBA can change sheet visibility.

Solution 1Recommended

Validate the selection before opening and hiding the worksheet

Use this approach for cells containing worksheet names, or assign a wrapper macro to a shape placed over a chart label.

Worksheet_SelectionChange responds to cell selections, not chart-label clicks. Restricting the event to a navigation range also prevents unrelated selections from being treated as sheet names.

The example assumes the main sheet is named Main and navigation cells are in B2:B20. Change those references to match your workbook. The hide-on-return state lasts only while the VBA project remains active.

1
Add the navigation macro

Press Alt+F11, choose Insert > Module, and paste this code: Public OpenedSheetName As String Public Sub OpenLinkedSheet(ByVal sheetName As String) Dim ws As Worksheet sheetName = Trim$(sheetName) If Len(sheetName) = 0 Or sheetName = "Main" Then Exit Sub If ThisWorkbook.ProtectStructure Then MsgBox "Unprotect the workbook structure first." Exit Sub End If On Error Resume Next Set ws = ThisWorkbook.Worksheets(sheetName) On Error GoTo 0 If ws Is Nothing Then Exit Sub On Error GoTo OpenError ws.Visible = xlSheetVisible ws.Activate OpenedSheetName = ws.Name Exit Sub OpenError: MsgBox "Could not open the worksheet: " & Err.Description End Sub

2
Add events to the main sheet

In the VBA editor's Project Explorer, double-click the Main worksheet and paste this code into its worksheet module: Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim v As Variant If Target.CountLarge <> 1 Then Exit Sub If Intersect(Target, Me.Range("B2:B20")) Is Nothing Then Exit Sub v = Target.Value2 If IsError(v) Then Exit Sub If VarType(v) <> vbString Then Exit Sub OpenLinkedSheet CStr(v) End Sub Private Sub Worksheet_Activate() Dim ws As Worksheet If Len(OpenedSheetName) = 0 Then Exit Sub If ThisWorkbook.ProtectStructure Then Exit Sub On Error Resume Next Set ws = ThisWorkbook.Worksheets(OpenedSheetName) On Error GoTo 0 If ws Is Nothing Then OpenedSheetName = "" Exit Sub End If On Error GoTo HideError ws.Visible = xlSheetHidden OpenedSheetName = "" Exit Sub HideError: MsgBox "Could not hide the worksheet: " & Err.Description End Sub

3
Set up and test cell navigation

Enter exact worksheet names in Main!B2:B20, then select a navigation cell to open its sheet. Return to the Main sheet tab to hide the opened sheet. If an existing hyperlink navigates elsewhere, remove it and use the cell selection as the trigger.

4
Provide a chart-label trigger if needed

For a chart label, insert a shape over the label and use its right-click Assign Macro command. First add a no-argument wrapper to the standard module, such as: Public Sub OpenSalesFromChartLabel() OpenLinkedSheet "Sales" End Sub Assign OpenSalesFromChartLabel to the shape and replace Sales with the target worksheet name.

Validate the selection before opening and hiding the worksheet
Selection behavior: Selecting a cell already selected does not trigger SelectionChange again. Select another cell first, or use an assigned shape macro for a clickable trigger.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for opening and editing spreadsheets. Its format compatibility helps you move everyday files from Microsoft Office; it does not fix Excel VBA event errors.

Open and edit spreadsheets in a familiar interfaceSupports Microsoft Excel formats, including XLSXOrganize workbook sheets for everyday navigationMove compatible Office documents into a lightweight workspace
QA img-9

Frequently Asked Questions

Why does Target.Value cause a type mismatch?

The selection may contain an error, multiple cells, or a value that is not a worksheet name. Check Target.CountLarge, test for errors and text, and confirm the worksheet exists before using it.

Will Worksheet_SelectionChange detect a chart-label click?

No. Worksheet_SelectionChange handles cell selections. To provide a chart-label trigger, place a shape over the label and use its right-click Assign Macro command to assign a wrapper macro.

Why can't the macro unhide a worksheet?

Workbook structure protection can prevent visibility changes. In Excel, check Review > Protect Workbook and remove structure protection before running the macro.

How do I keep the VBA code in the workbook?

Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). Open the file in desktop Excel and enable macros if you trust its source.