To hide a worksheet with VBA, set its Visible property to xlSheetHidden, for example: ThisWorkbook.Worksheets("Report").Visible = xlSheetHidden. Use xlSheetVeryHidden when the sheet should not appear in Excel’s ordinary Unhide list.
What hiding a worksheet means
Hiding a worksheet removes its tab from view without deleting the sheet or its contents. Formulas on other sheets can still refer to it, and VBA can still read or change it. Hiding is therefore useful for keeping supporting calculations, lookup tables, configuration values, or intermediate results out of the way while leaving them available to the workbook.
Excel has three worksheet visibility states:
xlSheetVisible: the sheet is displayed as a tab.xlSheetHidden: the sheet is hidden and can normally be restored through Excel’s Unhide command.xlSheetVeryHidden: the sheet is hidden and is not offered in the ordinary Unhide dialog. A user with access to the VBA editor can change this property back.
These are values of a worksheet’s Visible property, not different kinds of worksheets. Hiding a sheet does not protect its cells, conceal its data from someone who can inspect the workbook, or make its formulas private.
Hide a sheet without VBA
For a one-time change, using Excel’s interface is usually simpler than writing a macro. In Excel for Microsoft 365 on Windows, select the worksheet tab, then choose Home > Format > Hide & Unhide > Hide Sheet. To show a normally hidden sheet, choose Home > Format > Hide & Unhide > Unhide Sheet, select the sheet, and confirm.
Excel for Mac has the same basic hide and unhide capability, though the command’s placement can differ with the ribbon layout and version. Excel for the web can work with hidden sheets through its available interface, but it does not run VBA or provide the desktop VBA editor. If you need an automated action, open the workbook in the desktop Excel application.
Hide a worksheet with a VBA macro
In desktop Excel, open the VBA editor with Alt+F11 on Windows. On a Mac, use Option+F11; depending on the keyboard, you may also need to hold Fn. Insert a standard module, or use an existing one, and add a macro such as this. The example assumes the target worksheet is named Report:
Save a separate copy of the workbook before running a macro. VBA changes cannot be reversed with Ctrl+Z, so a backup is the safest way to recover from an unintended change.
Option Explicit
Sub HideReport()
' Hide the Report worksheet in the workbook containing this code.
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Report")
ws.Visible = xlSheetHidden
End Sub
Run the macro from the VBA editor or from Excel’s Macro dialog. If you want to run it from the ribbon, you can assign the macro to a button or another suitable control. The workbook must be saved in a macro-enabled format such as .xlsm or in a format that supports macros, such as .xlsb; saving as a standard .xlsx file does not preserve VBA code.
The code uses ThisWorkbook, which means the workbook containing the macro. That is often safer than ActiveWorkbook, because the active workbook can change when a user switches windows. The worksheet name must match the tab name exactly. If no worksheet with that name exists in the workbook, the macro will fail rather than hide a different sheet.
Use VeryHidden when appropriate
To keep a sheet out of the normal Unhide dialog, change the last line of the macro to ws.Visible = xlSheetVeryHidden. This can help keep an interface tidy when a workbook includes technical sheets that ordinary users should not need to restore during routine use. It is also useful when a macro is intended to control when a sheet is shown.
A VeryHidden sheet is not invisible to someone who knows how to inspect the workbook. In the VBA editor, select the worksheet in Project Explorer, open the Properties window, and change its Visible setting to 2 - xlSheetVeryHidden or back to -1 - xlSheetVisible. The Properties window can be opened with F4 in the editor. This method changes the same visibility property as the macro.
Choose VeryHidden for workflow and presentation, not confidentiality. If a user needs to reveal the sheet through the normal Excel interface, use xlSheetHidden instead. VeryHidden adds a step for someone trying to find the sheet, but it does not encrypt or secure the underlying information.
Choose the correct workbook and worksheet
A reliable macro identifies both the workbook and the sheet explicitly. This matters when several workbooks are open, when the active sheet changes, or when a macro is launched from a personal macro workbook or add-in. ThisWorkbook points to the file where the VBA code is stored; ActiveWorkbook points to the workbook currently in front. They may be the same, but they are not guaranteed to be.
The example uses the worksheet tab name, Report. If the tab is renamed, update the code to match. Another option is to refer to a worksheet’s VBA code name, which is visible in the VBA editor’s Project Explorer and is separate from the tab name. A code name can remain unchanged when someone renames the tab, but readers maintaining the workbook need to know which code name corresponds to which sheet.
Also make sure that the target is a worksheet. Worksheets refers to worksheet tabs, while Sheets can include other sheet types, such as chart sheets. For a normal grid-based worksheet, the Worksheets collection makes the intent clear. Avoid relying on whichever sheet happens to be active; explicit references reduce mistakes and make the macro easier to review.
Unhide a worksheet with VBA
A hidden worksheet can be restored by setting its visibility to xlSheetVisible. This code reverses the visibility change for the same named worksheet:
Option Explicit
Sub ShowReport()
' Make the Report worksheet visible in the workbook containing this code.
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Report")
ws.Visible = xlSheetVisible
End Sub
If the worksheet was set to VeryHidden, this macro still makes it visible; it does not matter whether the sheet is normally hidden or VeryHidden before the assignment. You can also restore it manually through the VBA editor’s Properties window by setting Visible to -1 - xlSheetVisible.
Excel must have at least one visible sheet in a workbook. If a macro tries to hide the only visible worksheet, Excel will not allow the workbook to end up with no visible sheets. A hide macro should therefore account for which sheets are currently visible and leave at least one visible. In multi-sheet workbooks, check the intended result before hiding the active or final visible tab.
Common errors and limits
If the macro produces an error, first check the name and workbook reference. A misspelled tab name, a worksheet that was deleted, or code running against a different workbook can prevent the reference from resolving. If the workbook’s structure is protected, Excel may also prevent sheet visibility changes. Workbook structure protection is different from protecting a worksheet’s cells: the first controls actions such as adding, moving, deleting, or hiding sheets, while the second controls edits to sheet content.
Other common causes include:
- Macros are blocked or disabled. Use a trusted source and follow your organization’s security settings. A workbook saved as
.xlsxwill not retain its VBA macro. - The wrong sheet is targeted. Confirm the tab name and whether the macro should use
ThisWorkbookor another workbook reference. - The sheet is the last visible one. Keep another worksheet visible before hiding the target.
- The workbook structure is protected. Remove or manage that protection with the appropriate authorization before changing sheet visibility.
VBA code can also be placed in a worksheet module, a standard module, or the workbook module, but where it is stored affects how it is called and maintained. For a simple macro that a user runs manually, a standard module is a clear choice. If you automate visibility when the workbook opens or when a user takes another action, event procedures may be suitable, but they add complexity and should be tested carefully.
Hiding is not data security
Neither Hidden nor VeryHidden should be used as a security boundary. A hidden sheet can still feed formulas, and a person with access to the workbook may be able to reveal it, inspect formulas, copy data, or use other tools to examine workbook contents. Protecting workbook structure can discourage accidental changes, but it should not be treated as strong protection for confidential information.
If information must be restricted, do not include it in a workbook distributed to people who should not see it. Use appropriate access controls and, where relevant, Excel’s file encryption features rather than relying on a hidden tab. For ordinary workbook design, hiding is best understood as a way to simplify the visible interface and reduce accidental interaction, not as a way to conceal sensitive data.
Alternatives to hiding a worksheet
Hiding is not always the best way to make a workbook easier to use. Consider the reader’s task and how often they need the underlying information:
- Group related sheets visibly. Descriptive tab names and consistent tab colors can make a multi-sheet workbook easier to navigate without concealing useful content.
- Protect cells or a worksheet from accidental edits. Sheet protection can limit editing actions, but it is not a substitute for confidentiality.
- Use a dedicated input or report sheet. Keep working data available while directing users to a clear front page or report tab.
- Remove unnecessary data from shared copies. If recipients should not receive the information at all, deleting it from the copy is safer than hiding it.
These approaches can be combined. For example, a workbook might present a simple report sheet, keep calculation sheets hidden to reduce clutter, and protect formulas against accidental edits. The right combination depends on whether the goal is navigation, preventing edits, or controlling access; those are different requirements.
Frequently Asked Questions
What is the VBA code to hide an Excel sheet?
Reference the worksheet and set its visibility to xlSheetHidden, such as ThisWorkbook.Worksheets("Report").Visible = xlSheetHidden. Use the worksheet’s exact tab name, and confirm the macro targets the intended workbook.
How do I hide a sheet so it cannot be unhidden in Excel?
Set Visible to xlSheetVeryHidden. This removes the sheet from the standard Unhide dialog, but it does not make the sheet impossible to reveal or provide data security.
How do I unhide a VeryHidden sheet?
In the VBA editor, select the worksheet and change its Visible property to -1 - xlSheetVisible, or run a macro that sets it to xlSheetVisible. The ordinary Unhide dialog does not list VeryHidden sheets.
Can VBA hide the only visible sheet?
No. Excel requires at least one visible sheet in a workbook, so arrange for another sheet to remain visible before hiding the target.
Can Excel for the web run a sheet-hiding VBA macro?
No. Excel for the web does not run VBA macros. Open the workbook in desktop Excel for Windows or Mac to use the VBA editor and run the macro.
Related
- How to Insert a Full-Width Horizontal Line in Word
- How to Insert a Fillable Checkbox in Microsoft Word
- Open a ZIP File in Excel: Extract and Import Data
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Range.Intersect: Find Overlapping CellsIn VBA, use Application.Intersect(range1, range2) to find the cells shared by two or more ranges. It returns a…
- Excel VBA Boolean: True, False, and How to Use ThemYes—Excel VBA has a built-in Boolean data type for values that are either True or False. Declare a…
- Copy and Paste Values in VBA: Methods and ExamplesTo copy only cell values with Excel VBA, assign one range’s Value2 to another range of the same…
- VBA Range Sum: Methods, Examples, and Common ErrorsTo sum a range in Excel VBA, use Application.WorksheetFunction.Sum and assign its result to a variable or cell.…
- VBA Range Address: Get, Set, and Understand ReferencesIn Excel VBA, use a range’s Address property to return its cell reference as text: for example, Range("B2:D5").Address…
- Google Sheets Macros: How They Work and What They Can DoYes. Google Sheets has macros that can record repeated actions and replay them later; unlike Excel macros, Sheets…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.