VBA macros · Sheets

VBA macro to hide all sheets except the active one in Excel

Hide every sheet except the one you are looking at with one VBA macro, with an option to make them very hidden so the Unhide menu cannot show them.

Windows + Mac

When you send a workbook to a client or present it on a projector, you often want only one sheet on screen: the summary, the dashboard or the invoice. Hiding the other sheets one by one takes a while, and you have to remember which ones to bring back. This macro hides every sheet except the active one in a single step.

VBA
Option Explicit

Sub HideAllSheetsExceptActive()
    ' Hides every sheet in the active workbook except the active sheet
    Dim sh As Object
    Dim keepName As String
    Dim hidden As Long

    If ActiveWorkbook Is Nothing Then Exit Sub

    If ActiveWorkbook.ProtectStructure Then
        MsgBox "The workbook structure is protected. Unprotect it first.", vbExclamation
        Exit Sub
    End If

    keepName = ActiveSheet.Name

    For Each sh In ActiveWorkbook.Sheets
        If sh.Name <> keepName And sh.Visible = xlSheetVisible Then
            sh.Visible = xlSheetHidden     ' use xlSheetVeryHidden to hide it from the Unhide menu
            hidden = hidden + 1
        End If
    Next sh

    MsgBox hidden & " sheet(s) hidden. Only '" & keepName & "' is visible.", vbInformation
End Sub
Hide all sheets except the active sheet

How to use it

  1. Open the workbook and press Alt+F11 (Mac: Opt+F11 or Fn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it.
  2. Choose Insert › Module. An empty code window opens on the right.
  3. Paste the whole code into that window. If the module already starts with Option Explicit, do not paste that line twice.
  4. Go back to Excel, press Alt+F8 (Mac: Opt+F8, or Tools › Macro › Macros), pick HideAllSheetsExceptActive and click Run.
  5. Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.

To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.

How it works

Before the loop starts, the macro stores the name of the active sheet in keepName. Comparing names is simple and reliable: sheet names are unique within a workbook, so exactly one sheet matches and stays visible.

The loop runs over ActiveWorkbook.Sheets, which includes chart sheets, so a chart sheet is hidden as well. The extra test sh.Visible = xlSheetVisible skips sheets that are already hidden. That matters for very hidden sheets: without the test they would be changed to normally hidden, which would make them show up in the Unhide dialog.

Excel insists that at least one sheet stays visible. Because the active sheet is never touched, the macro can never hit that rule. The workbook protection check at the top avoids an error message halfway through the loop, and the final message confirms the result. Objects and collections are explained in Workbooks and worksheets in VBA.

Change it

To keep a few sheets visible, list them in a string and test with InStr. The vertical bars around each name stop Sales from matching Sales 2025:

VBA
    Const KEEP As String = "|Dashboard|Read me|"

    For Each sh In ActiveWorkbook.Sheets
        If InStr(1, KEEP, "|" & sh.Name & "|", vbTextCompare) = 0 _
           And sh.Visible = xlSheetVisible Then
            sh.Visible = xlSheetHidden
            hidden = hidden + 1
        End If
    Next sh
Keep the sheets named in a list visible

Use xlSheetVeryHidden instead of xlSheetHidden if users should not unhide the sheets from the right-click menu. Very hidden sheets can only be unhidden from the VBA editor's Properties window or with code such as VBA macro to unhide all sheets in Excel. Pair this with workbook protection (Review › Protect Workbook) to stop people unhiding normally hidden sheets.

What can go wrong

Watch out. Hiding a sheet is not security. Anyone who knows VBA, or who opens the file in another program, can read hidden and very hidden sheets. Never rely on hiding to protect salaries, passwords or personal data; remove that data from the copy you send instead.

Formulas on the visible sheet keep working when the sheets they refer to are hidden; hiding only changes what you see. Charts based on hidden sheets also keep their data. If the macro stops with error 1004, check whether the workbook structure was protected after the check ran, or whether the active sheet is part of a group selection (several sheet tabs selected). Click a single tab first.

Tip. The macro works the same in Excel for Mac. Assign it to a button on the dashboard sheet if you hide and unhide often (right-click a shape › Assign Macro).

Related: VBA macro to unhide all sheets in Excel, VBA macro to protect or unprotect all sheets at once in Excel.

More vba macros