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
100 Excel VBA Macros

Do you know the fastest way to learn foreign languages?  It is to read, write, speak, and think in that language as often as possible.  Apart from speaking, programming languages are no different.  The more you immerse yourself in that language, the faster you will pick it up.

100 Excel Macros Book

Therefore, what most people like you need is lots of examples that you can practice.  That is why the 100 Excel VBA Macros eBook exists.  It’s the book for all Excel users who want to learn how to read and write Excel macros, save time, and stand out from their peers.  The book contains:

  • 100 example codes to practice reading and writing macros that will embed the language into your thinking.
  • An introduction to macros in Excel to ensure you can implement the VBA code in the book even if you have no prior knowledge.
  • Consistent code layout between examples to enable you to understand the structure and easily customize the code to meet your needs.
  • Downloadable workbook containing all the source code, so the examples can be added to your project to give you the benefit of VBA straight away.

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



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.

Email Address * First Name *

By entering your email address you agree to receive emails from Excel Off The Grid. We’ll respect your privacy and you can unsubscribe at any time.


Don’t forget:

If you’ve found this post useful, or if you have a better approach, then please leave a comment below.

Do you need help adapting this to your needs?

I’m guessing the examples in this post didn’t exactly meet 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 *