Skip to main content

Excel Macro: Hide All Worksheets Except Active One

Sometimes, you may need to hide all Worksheets except active one, if the Workbook have many Worksheets, it seems very boring to do it manually. In these situations, you can use this simple macro.

Hide All Worksheets Except Active One

'---------------- Modules ----------------
Sub HideAllWorksheetsExceptActive()
'Step 1:  Declare your variables
    Dim ws As Worksheet
'Step 2: Start looping through all worksheets
    For Each ws In ThisWorkbook.Worksheets
 
'Step 3: Check each worksheet name
        If ws.Name <> ThisWorkbook.Active Then
 
'Step 4: Hide the sheet
            ws.Visible = xlSheetHidden
        End If
   
'Step 5:  Loop to next worksheet
    Next ws
End Sub

How This Macro Works

  1. In Step 1 declares an object called ws. This creates a memory container for each worksheet the macro loops through.
  2. In Step 2 begins the looping, telling Excel to evaluate all worksheets in this workbook. There is a difference between ThisWorkbook and ActiveWorkbook. The ThisWorkbook object refers to the workbook that the code is contained in. The ActiveWorkbook object refers to the workbook that is currently active. They often return the same object, but if the workbook running the code is not the active workbook, they return different objects. In this case, we don’t want to risk hiding sheets in other workbooks, so we use ThisWorkbook.
  3. In Step 3, the macro simply compares the active sheet name to the sheet that is currently being looped.
  4. In Step 4, if the sheet names are different, the macro hides the sheet.
  5. In Step 5, we loop back to get the next sheet. After all of the sheets are evaluated, the macro ends.

You’ll notice that we used xlSheetHidden in our macro. This applies the default hide state you would normally get when you right-click a sheet and select Hide. In this default hide state, a user can right-click on any tab and choose Unhide. This shows all the sheets that are hidden. But there is another hide state that is more clandestine than the default. If you use xlSheetVeryHidden to hide your sheets, users will not be able to see them at all — not even if they right-click on any tab and choose Unhide. Read more about XlSheetVisibility enumeration.

The one way to unhide a sheet hidden in this manner is by using VBA: Unhide All Worksheets in a Workbook, the another way:

  1. Activate the Visual Basic Editor by pressing ALT+F11
  2. Click the Sheet name which you want to unhide in the Project window.
  3. Press F4 to show Properties Window.
  4. Select the sheet you want to unhide, in this case we select Sheet1.
  5. In Visible properties, click down arrow, then choose -1 - xlSheetVisible.

Most VBA code should be placed in Standard Modules unless specified.

If you see a comment '------------------ Modules------------------ in the code header that means put the code in a Standard Module. For more information, learn this course: Where should I put the Excel VBA code?

The following steps teach you how to put VBA code into a Standard Module:

  1. Activate the Visual Basic Editor by pressing ALT + F11.
  2. Right-click the project/workbook name in the Project Window.
  3. Choose Insert -> Module.
  4. Type or paste the code in the newly created module. You will probably need to change the sheet name, the range address, and the save location.
  5. Click Run button on the Visual Basic Editor toolbar.
  6. For more information, learn this course: Programming with Excel VBA

Leave a comment

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

Format your code: <pre><code class="language-vba">place your code here</code></pre>