How to Hide a Worksheet Using VBA in Excel

hide worksheet vba

If you need to make certain tabs invisible in Excel, you can use VBA to do this automatically. With a few lines of code, it’s possible to control the visibility of specific sheets without manual intervention. For example, you can set a worksheet to be hidden as soon as the workbook opens, or hide it based on specific criteria like user input or other conditions within the file.

Setting a worksheet as invisible: The code below shows how to quickly hide a sheet using the Visible property, which can be adjusted to make a tab invisible to users or even completely protect it from being revealed manually.

Preventing users from unhiding sheets: Adding protection to the sheet can ensure that others won’t accidentally unhide it. This is done through simple modifications in the VBA code, allowing for more secure management of sensitive data within your workbooks.

Automating sheet visibility: VBA allows you to create automated workflows, such as hiding certain tabs based on specific triggers like opening the workbook, changing values, or navigating between tabs. This is particularly useful for streamlining processes where certain data should remain hidden unless absolutely necessary.

How to Make a Tab Invisible Using VBA

hide worksheet vba

To make a tab disappear from the view, you can set its Visible property to xlSheetHidden. This will remove the sheet from the interface without deleting it, allowing you to keep the data intact while preventing users from accessing it directly. Below is the VBA code to achieve this:


Sub MakeSheetInvisible()
Sheets("SheetName").Visible = xlSheetHidden
End Sub

If you need to hide the sheet so that it cannot be unhidden by a user, use xlSheetVeryHidden instead. This option makes the sheet invisible and prevents it from being revealed through the Excel interface, even if users open the “Unhide” menu.


Sub MakeSheetVeryInvisible()
Sheets("SheetName").Visible = xlSheetVeryHidden
End Sub

Here’s how these options differ:

  • xlSheetHidden allows the sheet to remain hidden but visible in the “Unhide” dialog.
  • xlSheetVeryHidden makes the sheet completely inaccessible from the “Unhide” menu, ensuring it stays hidden even if the user tries to manually reveal it.

To make the sheet visible again, simply set its Visible property to xlSheetVisible.


Sub MakeSheetVisible()
Sheets("SheetName").Visible = xlSheetVisible
End Sub

How to Hide a Worksheet Using VBA Code

To make a tab invisible with VBA, you need to adjust the Visible property of the sheet. The following VBA code will make the specified sheet disappear from the user’s view:


Sub MakeSheetInvisible()
Sheets("SheetName").Visible = xlSheetHidden
End Sub

Once the sheet is set to xlSheetHidden, it will no longer be visible in the tab bar, but the data will still exist and can be accessed through other code. If you want to prevent users from un-hiding it, use xlSheetVeryHidden instead, which will make the sheet inaccessible via the Excel interface.

Visibility Option Result
xlSheetHidden The sheet is hidden, but users can unhide it through the “Unhide” option.
xlSheetVeryHidden The sheet is hidden and cannot be unhidden by users through Excel’s “Unhide” dialog.

To make the tab visible again, simply reverse the property like so:


Sub MakeSheetVisible()
Sheets("SheetName").Visible = xlSheetVisible
End Sub

Customizing Visibility: Hiding a Worksheet Based on Conditions

To control the visibility of a tab based on specific conditions, use a combination of If statements and VBA properties. This allows you to hide a tab dynamically depending on cell values, user inputs, or other criteria. For example, you can hide a sheet if a particular cell contains a specific value.


Sub ConditionalVisibility()
If Sheets("SheetName").Range("A1").Value = "Hide" Then
Sheets("SheetName").Visible = xlSheetHidden
Else
Sheets("SheetName").Visible = xlSheetVisible
End If
End Sub

This code hides the tab when the value in cell A1 is “Hide”, and reveals it otherwise. You can easily modify this to check for different conditions, such as comparing numerical values or text strings.

For more complex conditions, you can combine multiple criteria. For instance, hide the sheet if cell A1 equals “Hide” and cell B1 contains a specific date:


Sub ComplexCondition()
If Sheets("SheetName").Range("A1").Value = "Hide" And Sheets("SheetName").Range("B1").Value = "2026-01-01" Then
Sheets("SheetName").Visible = xlSheetHidden
Else
Sheets("SheetName").Visible = xlSheetVisible
End If
End Sub

This approach allows you to manage sheet visibility based on more intricate user-driven conditions or automatic triggers, giving you full control over when tabs should be shown or concealed.

Preventing Users from Unhiding Protected Worksheets with VBA

To prevent unauthorized access to hidden tabs, you can use password protection combined with a visibility setting. This ensures that even if a sheet is set to be xlSheetVeryHidden, users cannot unhide it manually from the “Unhide” dialog.

First, protect the workbook with a password. Then, implement the following code to make the sheet inaccessible and prevent users from unhiding it:


Sub ProtectSheetVisibility()
' Protect the workbook
ThisWorkbook.Protect Password:="yourPassword", Structure:=True
' Make the sheet very hidden
Sheets("SheetName").Visible = xlSheetVeryHidden
End Sub

When the workbook is protected, the user will not be able to unhide the sheet via Excel’s interface, even if the sheet’s visibility property is set to xlSheetHidden.

To reveal the sheet, you must unprotect the workbook first. Here’s how you can unlock the sheet and unprotect the workbook programmatically:


Sub UnprotectAndMakeVisible()
' Unprotect the workbook
ThisWorkbook.Unprotect Password:="yourPassword"
' Make the sheet visible
Sheets("SheetName").Visible = xlSheetVisible
End Sub

By using both protection and the xlSheetVeryHidden setting, you can ensure that sensitive tabs are not accessible or visible to unauthorized users.

Using VBA to Hide Worksheets During Excel Startup

To automatically conceal specific tabs when Excel is opened, you can place the necessary code inside the ThisWorkbook object’s Workbook_Open event. This method will trigger the hiding action every time the workbook is launched.

Here is an example of VBA code that hides a sheet as soon as the workbook is opened:


Private Sub Workbook_Open()
Sheets("SheetName").Visible = xlSheetHidden
End Sub

Place this code in the ThisWorkbook section of the VBA editor. Each time the file is opened, it will execute the code and hide the specified tab automatically.

If you want the tab to remain inaccessible even after it is hidden, use the xlSheetVeryHidden setting instead of xlSheetHidden. This will ensure that the sheet cannot be revealed by the user through Excel’s “Unhide” menu:


Private Sub Workbook_Open()
Sheets("SheetName").Visible = xlSheetVeryHidden
End Sub

By placing the code in the Workbook_Open event, it runs as soon as the workbook is loaded, keeping the sheet hidden unless explicitly unprotected and made visible via VBA code.

How to Hide a Worksheet Using VBA in Excel

How to Hide a Worksheet Using VBA in Excel