Get our FREE VBA eBook of the 30 most useful Excel VBA macros.

Automate Excel so that you can save time and stop doing the jobs a trained monkey could do.

Claim your free eBook

VBA to hide all sheets except one

VBA Code Snippets

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.

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


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 which may be useful to you are:

Headshot Round

About the author

Hey, I’m Mark, and I run Excel Off The Grid.

My parents tell me that at the age of 7 I declared I was going to become a qualified accountant. I was either psychic or had no imagination, as that is exactly what happened. However, it wasn't until I was 35 that my journey really began.

In 2015, I started a new job, for which I was regularly working after 10pm. As a result, I rarely saw my children during the week. So, I started searching for the secrets to automating Excel. I discovered that by building a small number of simple tools, I could combine them together in different ways to automate nearly all my regular tasks. This meant I could work less hours (and I got pay raises!). Today, I teach these techniques to other professionals in our training program so they too can spend less time at work (and more time with their children and doing the things they love).

Do you need help adapting this post to your needs?

I'm guessing the examples in this post don't exactly match your situation. We all use Excel differently, so it's impossible to write a post that will meet everybody's needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site), you should be able to adapt it to your needs.

But, if you're still struggling you should:

  1. Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
  2. Ask the 'Excel Ninja' in your office. It's amazing what things other people know.
  3. Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it's clear and concise.  List all the things you've tried, and provide screenshots, code segments and example workbooks.
  4. Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.

What next?
Don't go yet, there is plenty more to learn on Excel Off The Grid.  Check out the latest posts:

Leave a Reply

Your email address will not be published. Required fields are marked *