VBA macros · Sheets

VBA macro to unhide all sheets in Excel

A ready-to-use VBA macro that unhides every hidden and very hidden sheet in a workbook in one go, with a check for workbook protection.

Windows + Mac

Excel lets you hide many sheets at once, but in older versions you can only unhide them one at a time through the Unhide dialog. With twenty hidden sheets that is twenty trips to the same menu. The macro below makes every sheet in the active workbook visible in one run, including very hidden sheets that the Unhide dialog does not even list, and tells you how many it changed.

VBA
Option Explicit

Sub UnhideAllSheets()
    ' Makes every worksheet and chart sheet in the active workbook visible
    Dim sh As Object
    Dim changed As Long

    If ActiveWorkbook Is Nothing Then Exit Sub

    ' Hiding and unhiding is blocked while the workbook structure is protected
    If ActiveWorkbook.ProtectStructure Then
        MsgBox "The workbook structure is protected." & vbLf & _
               "Unprotect it first (Review > Protect Workbook).", vbExclamation
        Exit Sub
    End If

    For Each sh In ActiveWorkbook.Sheets
        If sh.Visible <> xlSheetVisible Then
            sh.Visible = xlSheetVisible
            changed = changed + 1
        End If
    Next sh

    MsgBox changed & " sheet(s) unhidden.", vbInformation
End Sub
Unhide every sheet in the active workbook

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 UnhideAllSheets 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

ActiveWorkbook.Sheets is the collection of all sheets: normal worksheets and chart sheets. That is why the loop variable sh is declared As Object rather than As Worksheet; a chart sheet would not fit into a Worksheet variable and the loop would stop with a type mismatch.

Every sheet has a Visible property with three possible values: xlSheetVisible (-1), xlSheetHidden (0) and xlSheetVeryHidden (2). The test sh.Visible <> xlSheetVisible catches both kinds of hidden sheet, and setting the property to xlSheetVisible brings the sheet back. The counter changed only goes up for sheets that really were hidden, so the final message tells you what the macro actually did.

The check on ProtectStructure comes first because a protected workbook structure blocks hiding, unhiding, renaming and moving sheets. Without the check the macro would stop with a run-time error on the first hidden sheet. Loops over collections are explained in For loops: For...Next and For Each, and the workbook and sheet objects in Workbooks and worksheets in VBA.

Change it

To unhide only some sheets, add a condition on the name. The Like operator understands * (any text) and ? (one character), so this version only unhides sheets whose name contains 2026:

VBA
    For Each sh In ActiveWorkbook.Sheets
        If sh.Visible <> xlSheetVisible And sh.Name Like "*2026*" Then
            sh.Visible = xlSheetVisible
            changed = changed + 1
        End If
    Next sh
Only unhide sheets whose name contains 2026

To leave very hidden sheets alone (they are often hidden on purpose by the person who built the file), change the test to If sh.Visible = xlSheetHidden Then. To work on the workbook that contains the code instead of the active one, replace ActiveWorkbook with ThisWorkbook. The reverse job is covered by VBA macro to hide all sheets except the active one in Excel.

What can go wrong

Watch out. Very hidden sheets are often used by templates and add-ins to store settings or lookup lists. Unhiding them is harmless, but do not delete or edit them unless you know what they do. If a file stops working after you unhid its sheets, close it without saving.

If the structure is protected with a password you do not know, the macro cannot help; ask the owner of the file. If you get error 1004 on a particular sheet, that sheet may belong to an add-in or a shared workbook. Run the macro from the workbook you want to change: when the VBA editor has focus, ActiveWorkbook is still the workbook that was active in Excel, which is usually what you want.

Tip. In Microsoft 365 and Excel 2021 the Unhide dialog finally lets you select several sheets with Ctrl+click (Mac: Cmd+click). The macro is still faster when you do this often, and it is the only quick way to reveal very hidden sheets. It works the same on Windows and Mac.

Related: VBA macro to hide all sheets except the active one in Excel, VBA macro to list all sheet names in Excel, Workbooks and worksheets in VBA.

More vba macros