VBA to hide all sheets except one

There is a common question that many Excel users ask: “How can I hide all sheets except one with a macro?”. So, let’s answer that very question. First, I’ll share with you the VBA to hide all sheets except one, and then the process to turn it into a reusable code block.

Table of Contents

Basic VBA code

Enter the following code into a standard code module:

Sub HideAllWorksheetsExceptOne()

'Create a variable to hold worksheets
Dim ws As Worksheet

'Create a variable to hold the worksheet name to keep
Dim wsKeepName As String

'Store the name of the sheet to keep
wsKeepName = ActiveSheet.Name

'Loop through each worksheet in the workbook
For Each ws In ActiveWorkbook.Worksheets

    'If the ws in the loop is not the active sheet then
    If ws.Name <> wsKeepName Then

        'Make the worksheet hidden
        ws.Visible = xlSheetHidden

    End If

Next ws

End Sub

We will adapt and change this code in the following sections to make it more flexible for our needs.

Adapting the code

The specific example above will hide all sheets except the active sheet within the active workbook. But that may not be what you need for your scenario. For this macro, there are 3 different settings that we can change:

  • Worksheet to keep visible
  • Workbook to apply the macro to
  • Making worksheets hidden or very hidden

Let’s take a look at these three options in turn.

Change the worksheet

To refer to a specific worksheet, we can use one of the following lines of code:

Active Sheet:

This is the code currently used in the example. If you want to change the code, switch this line to one of the lines below.

wsKeepName = ActiveSheet.Name

Named Sheet:

wsKeepName = "MySheetName"

Replace MySheetName with the name of your sheet.

Nth sheet position:

wsKeepName = Sheets(1).Name

The number refers to the sheet position, where 1 is the first sheet, 2 is the second sheet, etc. Be aware, the numbering refers to all sheets, whether they are hidden or visible. Therefore, if Sheets(1) is hidden, it will not be displayed in the tabs at the base of the Excel interface.

Change the workbook

The VBA code can also be adapted to work on any open workbooks.

Active workbook:

This is the code currently used in the example. If you want to change the code, switch this line to one of the lines below.

For Each ws In ActiveWorkbook.Worksheets

Named workbook:

For Each ws In Workbooks("WorkbookName.xlsx").Worksheets

Remember to change WorkbookName.xlsx for the name of your workbook.

Nth workbook opened

For Each ws In Workbooks(1).Worksheets

The number refers to workbooks in the order opened, where 1 is the first workbook, 2 is the second workbook, etc.

Change between hidden and very hidden

The initial example hides the worksheets, which means they can be unhidden by the user with the standard interface. We can also set a worksheet to xlSheetVeryHidden; these are not displayed in the hidden sheet list and can only be made visible through more advanced methods.

Make sheets hidden

ws.Visible = xlSheetHidden

Make sheets very hidden

ws.Visible = xlSheetVeryHidden

Creating a reusable procedure

Often it is better to have a single reusable code block into which we can pass relevant variables; this is what the code below achieves. The worksheet name, workbook name, and visibility settings are passed into the macro as arguments.

Sub HideAllWorksheetsExceptOne(wsKeepName As String, wbName As String, _
    visibilityType As Long)

'Create a variable to hold worksheets
Dim ws As Worksheet

'Loop through each worksheet in the workbook
For Each ws In Workbooks(wbName).Worksheets

    'If the ws in the loop is not specified sheet then
    If ws.Name <> wsKeepName Then

        'Make each worksheet hidden
        ws.Visible = visibilityType

    End If

Next ws

End Sub

We cannot run this code by itself, as we must pass variables into it.  The code below shows an example of how to call the reusable procedure. All the arguments in the brackets below are the three settings to be applied.

Sub CallTheReusableCode()

Call HideAllWorksheetsExceptOne("MySheetName", "WorkbookName.xlsx", xlSheetHidden)

End Sub

Notes

Here are a few things to take into consideration:

  • There needs to be at least one visible worksheet. If we try to hide the last visible worksheet, it will cause an error.
    Error when trying to hide the last worksheet
  • It may be necessary to run a macro to unhide all worksheets first. This will ensure the worksheet to be retained is not already hidden.
  • From a VBA perspective, Sheets and Worksheets are not the same. Sheets is a collection that contains all worksheets and chart sheets. The examples above refer to worksheets. If your workbook contains chart sheets, then it will be necessary to adapt the code further. Check out this post for more detail: The difference between Sheets and Worksheets in VBA.

Related pages:


Discover how you can automate your work with our Excel courses and tools.

Excel Academy

Excel Academy
The complete program for saving time by automating Excel.

Excel Automation Secrets

Excel Automation Secrets
Discover the 7-step framework for automating Excel.

Office Scripts Course

Office Scripts: Automate Excel Everywhere
Start using Office Scripts and Power Automate to automate Excel in new ways.

Leave a Comment