To hide a worksheet with VBA, set its Visible property to xlSheetHidden, for example Worksheets("Sheet2").Visible = xlSheetHidden. To make it disappear completely so users cannot unhide it from the Excel interface, use xlSheetVeryHidden instead. Both go in a standard code module, and both take effect the moment the line runs. A workbook must always contain at least one visible sheet, and hiding a sheet renames its tab area, so anything that refers to that sheet by name in a formula or macro will break until you unhide it.
What You'll Need
- Excel on Windows or Mac with macros enabled (any current desktop version works; Excel for the web cannot run VBA at all).
- A workbook saved as
.xlsm(Excel Macro-Enabled Workbook) or.xlsb, so the code survives a save and reopen. - Trust access to the VBA project is not required just to run code you typed yourself, but macro security must allow macros to run. Check this in File > Options > Trust Center > Trust Center Settings > Macro Settings. Labels vary slightly by version and platform.
- A sheet that is not the only visible sheet in the workbook, and not protected in a way that blocks changes.
Step-by-Step
Save a copy of the workbook first. Hiding sheets is easy to reverse, but if you hide the sheet your other macros write to, you can produce confusing errors or overwrite data before you notice. Keep the original somewhere safe.
Open the VBA editor. Press Alt+F11 on Windows, or Fn+Option+F11 on a Mac, or go to Developer > Visual Basic. If the Developer tab is not showing, enable it in File > Options > Customize Ribbon by ticking Developer in the right-hand list.
Insert a standard module. In the editor, use Insert > Module. Do not paste the code into a sheet module or the ThisWorkbook module unless you explicitly want it tied to an event such as opening the workbook.
Write the hide statement. In the empty module, type a short procedure:
Sub HideSheet() Worksheets("Sheet2").Visible = xlSheetHidden End SubThe sheet name in quotes must match the tab name exactly, including spaces. If you are unsure of the name, look at the Project Explorer on the left or the tab at the bottom of the Excel window.
Run it. Click anywhere inside the procedure and press F5, or use Run > Run Sub/UserForm. Switch back to Excel (Alt+F11 again) and the tab should be gone from the tab strip.
Use Very Hidden when hiding must stick. Change the line to
Worksheets("Sheet2").Visible = xlSheetVeryHidden. This is not offered anywhere in the Excel interface, so a user right-clicking a tab and choosing Unhide will not see the sheet listed. Only VBA (or the Properties window in the editor) can bring it back.Target the sheet by code name instead of tab name. If users may rename tabs, use the sheet's code name, which does not change:
Sheet2.Visible = xlSheetHidden. You can see each sheet's code name in the Project Explorer, and edit it in the Properties window.Hide by index only if the order never changes.
Worksheets(2).Visible = xlSheetHiddenhides the second tab from the left. It is fragile, because inserting or moving a sheet silently changes which sheet you hit. Prefer names or code names in anything you will keep.Hide several sheets in one pass. Loop over a range of names:
Sub HideMany() Dim ws As Worksheet For Each ws In ThisWorkbook.Worksheets If ws.Name <> "Dashboard" Then ws.Visible = xlSheetHidden End If Next ws End SubNote the exception: at least one sheet must stay visible, so this pattern always keeps a named sheet on screen.
Hide automatically when the workbook opens. If you want the sheet hidden every time the file is opened, put the statement in the ThisWorkbook module inside
Private Sub Workbook_Open(). Save the workbook after adding it, because the event runs on open, not when you type it.Unhide when you need it back. Run
Worksheets("Sheet2").Visible = xlSheetVisible. If the sheet is very hidden, this is essentially the only way to get it back, which is exactly why it is used.
Common Mistakes
- Hiding every sheet. VBA throws error 1004 if the last visible sheet is hidden. Always leave one visible.
- Assuming a hidden sheet is protected. Hiding is cosmetic. Anyone can unhide a normally hidden sheet and read or change it. Very hidden is stronger but still not security; if the data is confidential, protect the workbook structure with a password (Review > Protect Workbook) and be aware that passwords are not unbreakable.
- Confusing
HiddenwithVisible. There is no.Hiddenproperty on a worksheet. OnlyVisibleexists, with constants xlSheetVisible, xlSheetHidden and xlSheetVeryHidden. - Using
ActiveSheet. If the wrong sheet is active when the macro runs, you hide the wrong sheet. Always name the target. - Forgetting
ThisWorkbook. PlainWorksheets(...)refers to the active workbook. Another open file can be the active one, so the code fails or hides the wrong sheet. UseThisWorkbook.Worksheets(...)when the macro belongs to a specific file. - Formulas and links break. Other sheets referencing the hidden sheet keep working, but a chart series or a macro that selects the sheet can fail because you cannot select a hidden sheet.
- Saving as
.xlsx. That format strips macros on save, so the next open shows no code and no hidden state changes.
Tips
- Use very hidden for settings sheets, lookup tables and helper sheets that should never be touched. It keeps the workbook tidy without clutter on the tab strip.
- Store the state before changing it so you can restore it exactly:
Dim oldState As XlSheetVisibility: oldState = ws.Visible. - If you need the sheet's data but not the tab, another approach is to move data to a very hidden sheet and read it with normal cell references. Hidden sheets still calculate.
- Group related code: one
HideAlland oneShowAllprocedure makes a workbook easy to hand over to someone else. - If you hide by name, add a friendly error message so a renamed tab is obvious:
If Not SheetExists("Sheet2") Then MsgBox "Sheet not found". You can test withOn Error Resume Next: Set ws = ThisWorkbook.Worksheets("Sheet2"). - Very hidden sheets are still listed in the VBA editor's Project Explorer and can be revealed with the Properties window. Do not put anything there that must stay private from a determined user.
Troubleshooting
Error 1004, "Unable to set the Visible property of the Worksheet class". You are trying to hide the last visible sheet, or the sheet is protected with structure protection on. Unhide or add a sheet, and remove structure protection under Review > Protect Workbook if you have the password.
Error 9, "Subscript out of range". The sheet name in quotes does not exist in that workbook. Check spelling, spaces and that the workbook is the one you think it is.
Nothing happens when you press F5. The cursor may be outside a procedure, or the code is in a sheet or workbook module that is not being triggered. Click inside the Sub and End Sub block and run again.
The tab is still there. The code ran on a different workbook. Qualify with ThisWorkbook so it always targets the file that holds the macro.
The sheet vanished and you cannot find it. Right-click any visible tab and choose Unhide. If it is not listed, it is very hidden: open the VBA editor, select the sheet in the Project Explorer, set Visible to xlSheetVisible in the Properties window (F4), or run a one-line macro setting it to xlSheetVisible.
Frequently Asked Questions
What is the difference between Hidden and Very Hidden?
A hidden sheet can be restored by any user through the Unhide menu. A very hidden sheet cannot be reached from the Excel interface at all; only VBA or the Properties window can make it visible again.
Can I password-protect a hidden sheet?
Hiding itself has no password. To stop users unhiding and editing, protect the workbook structure with a password via Review > Protect Workbook. A visible sheet can separately be protected from edits with Review > Protect Sheet.
Will hiding a sheet break formulas that reference it?
No. Formulas and charts that read from a hidden sheet continue to calculate normally. Only actions that require the sheet to be active, such as selecting it, will fail.
Can I hide a sheet without macros?
Yes, right-click the tab and choose Hide. That produces a normal hidden sheet. Very hidden requires VBA or the VBA Properties window.
Why does the error say I cannot hide the last sheet?
Excel requires at least one visible worksheet in every workbook. Add a new sheet or unhide another one before running the macro again.
Does hiding a sheet reduce file size?
No. The data stays in the file, and hidden sheets still calculate. Only deleting or moving data to another workbook reduces size.