Using Excel VBA to Hide Multiple Sheets – 9 Methods

 

Introduction to the Worksheet.Visible Property in Excel VBA

The Worksheet.Visible property hides multiple sheets in an Excel file. The Visible property can have one of the following values:

  • xlSheetVisible: The xlSheetVisible value is the default value: the worksheet is visible by default.
  • xlSheetHidden: Hides a worksheet.
  • xlSheetVeryHidden: It is similar to the xlSheetHidden, but has a stronger level of protection. The worksheet will not be visible in the Excel user interface, and can only be unhidden using a VBA code.

 

Method 1 – Hiding Multiple Sheets by Mentioning Each Sheet Name in the VBA code

Hide 4 of the 5 Sheets:

Five Worksheets visible

  • Use the following code.
Sub ExplicitNameMention()
'declaring variables
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim ws4 As Worksheet
Set ws1 = Worksheets("Sales")
Set ws2 = Worksheets("Revenue")
Set ws3 = Worksheets("Profit")
Set ws4 = Worksheets("Sheet_Names")
'hide four worksheets at the same time
ws1.Visible = xlSheetHidden
ws2.Visible = xlSheetHidden
ws3.Visible = xlSheetHidden
ws4.Visible = xlSheetHidden
End Sub

excel vba Code to hide multiple sheets

VBA Code Breakdown

In this code,

  • declares 4 variables ws1, ws2, ws3, and ws4 as the Worksheet object.
  • sets the values of the Worksheet variables to the 4 Sheets you want to hide.
  • transforms the .Visible property into xlSheetHidden.

The sheets will be hidden.

Only one Worksheet visible after running the Code Excel VBA hide multiple Sheets


Method 2 – Hide Multiple Sheets Using a List from Another Sheet

Take the names of the sheets to hide from another sheet:

Five Worksheets  visible before running the Code

  • Use the following code.
Sub List_From_Another_Sheet()
'setting variable values
Set ws1 = Worksheets("List_From_Another_Sheet").Range("B5")
Set ws2 = Worksheets("List_From_Another_Sheet").Range("B6")
Set ws3 = Worksheets("List_From_Another_Sheet").Range("B7")
Set ws4 = Worksheets("List_From_Another_Sheet").Range("B8")
'hide four worksheets at the same time
Worksheets(ws1.Value).Visible = xlSheetHidden
Worksheets(ws2.Value).Visible = xlSheetHidden
Worksheets(ws3.Value).Visible = xlSheetHidden
Worksheets(ws4.Value).Visible = xlSheetHidden
End Sub

Code to hide multiple Sheets from list

VBA Code Breakdown

  • the names of the worksheets in B5:B8 of the List_From_Another_Sheet are assigned as the value of the 4 Worksheet variables.
  • the .Visible property of each sheet is set to xlSheetHidden.

The sheets in the list are not visible.

One Worksheet visible


Method 3 – Hiding Multiple Sheets Using a For Loop

Use a For Loop VBA  to hide sheets:

Five Worksheets visible before executing For Loop

  • Use the following code.
Sub Using_For_Loop()
'declaring variable
Dim ws As Worksheet
'setting value of the variable
Set Rng = Worksheets("For_Loop").Range("B5:B8")
'running for loop
For Each Cell In Rng
    Worksheets(Cell.Value).Visible = True
Next Cell
End Sub

Code to run For Loop to hide Sheets

The above code declares B5:B8 of the For_Loop Sheet and in each iteration, it sets the .Visible property of the Sheets in the list to xlSheetHidden.

Only the Sheet that is not in the list will be visible.

One Worksheet visible after running For Loop


Method 4 – Hiding Multiple Sheets with a Specific Tab Color

There are two sheets with a specific tab color (light green, here). To hide these sheets:

Two Worksheets with specific color

  • Use the following code.
Sub Tab_Color()
'declaring variables
Dim tabcolor As Long
Dim ws As Worksheet
'setting the numeric value of the light green color as the_
'_tabcolor value
tabcolor = 5296274
'running for loop to hide the sheets with light green tabcolor
For Each ws In ActiveWorkbook.Sheets
    If ws.Tab.Color = tabcolor Then
        ws.Visible = xlSheetHidden
    End If
Next ws
End Sub

Code to hide Worksheets with specific tab color

The above code runs a For Loop through the worksheets in the workbook and matches their tab color with a specific tab color (light green). If the tab color is light green, the code hides the sheet.

Sheets with a light green tab are hidden.

Worksheets with specific tab color are hidden


Method 5 – Using a Specific Value to Hide Multiple Sheets

Hide multiple Sheets by using a specific value. Here, Hide in B15.

Worksheet containing a specific value

  • Use the following code.
Sub Specific_Value()
'declaring variable
Dim ws As Worksheet
'running For Loop through each sheet
For Each ws In ActiveWorkbook.Sheets
    If ws.Range("B15").Value = "Hide" Then
        ws.Visible = xlSheetHidden
    End If
Next ws
End Sub

VBA Code to hide Sheets with a specific value

A For Loop iterates through the sheets and checks if the value in B15 is equal to “Hide”. If it is, the code hides the sheet.

Two of the five sheets in the workbook are hidden.

Sheets with specific value are hidden


Method 6 – Using the Sheet Properties Tab to Hide Multiple Sheets

Set the Visible property to xlSheetHidden.

Visible Worksheets before changing Visible property

  • Click any sheet name in the Microsoft Excel Object tab.
  • Go to the Properties tab.
  • Set Visible to xlSheetHidden.

Changing Visible property

Two sheets set to xlSheetHidden are hidden.

Three Sheets visible after changing Visible property


Method 7 – Hiding Multiple Sheets using the xlSheetVeryHidden Option

All the visible Sheets before running the Code

  • Use the following code.
Sub Very_Hidden()
'declaring variables
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim ws4 As Worksheet
'setting variable values
Set ws1 = Worksheets("Sales")
Set ws2 = Worksheets("Sheet_Name")
Set ws3 = Worksheets("Revenue")
Set ws4 = Worksheets("Profit")
'hiding 4 sheets at a time
ws1.Visible = xlSheetVeryHidden
ws2.Visible = xlSheetVeryHidden
ws3.Visible = xlSheetVeryHidden
ws4.Visible = xlSheetVeryHidden
End Sub

Code to apply VeryHidden property to hide Sheets

The code calls each sheet and hides it, setting the .Visible property to xlSheetVeryHidden.

Only one sheet is visible.

One Sheet visible after running the Code

Read More: How to Unhide All Sheets in Excel Using VBA


Method 8 – Hiding All Sheets Excluding the Active Sheet

Here, the name of the active sheet is Active_Sheet. After running the code only this sheet will be visible.

Four Sheets visible along with the ActiveSheet

  • Use the following code.
Sub Except_ActiveSheet()
'declaring variable
Dim ws As Worksheet
'running For Loop to hide sheets other than the ActiveSheet
For Each ws In ActiveWorkbook.Sheets
    If ws.Name = ActiveSheet.Name Then
        ws.Visible = xlSheetVisible
    Else
        ws.Visible = xlSheetHidden
    End If
Next ws
End Sub

Code to hide all the Sheets except ActiveSheet

VBA Code Breakdown

a loop to iterate through all the sheets in the Workbook and checks whether the name of the sheet is Active_Sheet. If there’s a match, it will be visible. If the name doesn’t match, the sheet is hidden.

  • This is the output.

Only ActiveSheet visible

Read More: VBA to Hide All sheets Except One in Excel


Method 9 – Hiding Multiple Sheets with a Specific Name

To hide sheets with a specific name (here, Sheet1 and Sheet2):

Sheets with specific name visible

  • Use the following code.
Sub Specific_Name()
'declaring variable
Dim ws As Worksheet
'running For Loop to hide sheet with specific name
For Each ws In ActiveWorkbook.Sheets
    If Left(ws.Name, Len(ws.Name) - 1) = "Sheet" Then
        ws.Visible = xlSheetHidden
    Else
        ws.Visible = xlSheetVisible
    End If
Next ws
End Sub

VBA Code to hide Sheets with specific name

The code iterates through all the sheets and checks whether the characters starting from the left in the name of the sheet (excluding the last character) match the word “sheet”. If they do, the sheet is hidden.

This is the output.

Sheets with specific name are hidden


Hiding Multiple Sheets from a Protected Workbook in Excel VBA

A protected workbook cannot be unhidden. The hide command is disabled if you right-click the sheet name.

The following code first unprotects the workbook and then hides multiple sheets:

Hiding Sheets from protected Workbook

  • Use the code:
Sub Protected_WorkBook()
'unprotecting workbook with password 123
ThisWorkbook.Unprotect ("123")
Dim ws As Worksheet
Set Rng = Worksheets("Sheet_Name").Range("B5:B7")
'running For Loop to hide the Sheets
For Each Cell In Rng
    Worksheets(Cell.Value).Visible = False
Next Cell
End Sub

VBA Code to hide Sheets from protected Workbook

VBA Code Breakdown

The code unprotects the workbook using the Unprotect method with the password “123”. It declares the “ws” variable as a worksheet. The code sets a range (“Rng”) to refer to B5: B7 in a sheet named “Sheet_Name”. This range is used to determine which sheets will be hidden.

Inside the For Loop, the code iterates through each cell in the range referred to by “Rng”. It hides the sheet whose name matches the value in the cell.

Sheets in the list are hidden. The Hide command is enabled.

Sheets from protected Workbook are hidden


How to Hide and Unhide Sheets in Excel with a VBA Button

Hide and unhide multiple Sheets with VBA Buttons:

  • Go to Developer >> Insert.
  • Select Button in ActiveX Controls.
  • Insert the Button in the sheet.

Inserting Command Button

  • Here, two Buttons will be inserted and renamed.
  • Right-click one of the Buttons and choose View Code.
  • In the code module, use the following codes.

Accessing Command Button codes

 

Private Sub CommandButton1_Click()
Set ws1 = Worksheets("Sheet_Name").Range("B5")
Set ws2 = Worksheets("Sheet_Name").Range("B6")
Set ws3 = Worksheets("Sheet_Name").Range("B7")
Set ws4 = Worksheets("Sheet_Name").Range("B8")
'hide four worksheets at the same time
Worksheets(ws1.Value).Visible = xlSheetHidden
Worksheets(ws2.Value).Visible = xlSheetHidden
Worksheets(ws3.Value).Visible = xlSheetHidden
Worksheets(ws4.Value).Visible = xlSheetHidden
End Sub
Private Sub CommandButton2_Click()
Set ws1 = Worksheets("Sheet_Name").Range("B5")
Set ws2 = Worksheets("Sheet_Name").Range("B6")
Set ws3 = Worksheets("Sheet_Name").Range("B7")
Set ws4 = Worksheets("Sheet_Name").Range("B8")
'hide three worksheets at the same time
Worksheets(ws1.Value).Visible = xlSheetVisible
Worksheets(ws2.Value).Visible = xlSheetVisible
Worksheets(ws3.Value).Visible = xlSheetVisible
Worksheets(ws4.Value).Visible = xlSheetVisible
End Sub

Code to hide and unhide Sheets using Command Button

VBA Code Breakdown

Private Sub CommandButton1_Click() is an Event. When you click the Hide Button, the code in this Subroutine will run, take the sheet names in B5:B8 and assign them to the Worksheet variables. The .Visible property of each Sheet is set to xlSheetHidden.

The Private Sub CommandButton2_Click() is also an Event. The Subroutine will run if you click the Unhide Button. It will unhide all hidden Sheets.


Frequently Asked Questions

  • What is the difference between Sheets () and Worksheets () in VBA?

In VBA (Visual Basic for Applications), both “Sheets()” and “Worksheets()” refer to the collection of worksheets in a workbook in Excel. However, there is a subtle difference between the two.

“Worksheets()” refers specifically to the collection of worksheets that contain cells in a grid. It excludes chart sheets, macro sheets, and other types of sheets that don’t have cells in a grid.

“Sheets()” refers to the collection of all sheets in the workbook, including worksheets, chart sheets, macro sheets, and any other type of sheet.

  • Can I hide sheets and prevent other users from seeing them?

Yes, use the Application.ScreenUpdating property to prevent users from seeing the hidden sheets. Set the property to False before hiding the sheets and then set it back to True. The code below is an example:

Sub HideSheetsWithoutUserSeeing()
Application.ScreenUpdating = False
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Visible = xlSheetHidden
Next ws
Application.ScreenUpdating = True
End Sub
  • How can I hide sheets when the workbook is open?

Use the Workbook_Open event to automatically hide sheets. The code below is an example:

Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Visible = xlSheetHidden
Next ws
End Sub

Download Practice Workbook

Download the practice book here.


 

Get FREE Advanced Excel Exercises with Solutions!
Adnan Masruf
Adnan Masruf

Adnan Masruf, holding a BSc in Naval Architecture & Engineering from Bangladesh University of Engineering and Technology, plays a pivotal role as an Excel & VBA Content Developer at ExcelDemy. His deep passion for research and innovation seamlessly aligns with his dedication to Excel. In this capacity, Masruf not only skillfully addresses challenging issues but also exhibits enthusiasm and expertise in gracefully navigating intricate situations, underscoring his steadfast commitment to consistently delivering exceptional content. His interests... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo