Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Worksheet Visible: Step-by-Step

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To make a worksheet visible (or hidden) with Excel VBA, set its Visible property to xlSheetVisible or xlSheetHidden. For example, Worksheets("Sheet2").Visible = xlSheetVisible unhides Sheet2, and Worksheets("Sheet2").Visible = xlSheetHidden hides it. Use xlSheetVeryHidden to hide a sheet so it cannot be unhidden from the Excel interface, only through VBA or the Properties window. This guide walks through the exact code, the places to put it, and the errors that trip people up.

What You'll Need

  • Excel on Windows or Mac (the VBA editor is included in both).
  • A workbook saved as .xlsm (macro-enabled) if you want the code to persist.
  • At least one worksheet in the workbook.
  • Basic familiarity with opening the VBA editor (Alt+F11 on Windows, Option+F11 on Mac).

Step-by-Step

  1. Open the VBA editor. In Excel, press Alt+F11 (Windows) or Option+F11 (Mac). The Visual Basic Editor opens in a separate window. Menu labels and dialog wording can differ slightly by Excel version, platform and region.
  2. Insert a module. In the editor, choose Insert > Module. A blank code window appears. You can also double-click ThisWorkbook in the Project pane if you want the code to run on workbook events, but a standard module is fine for a one-off macro.
  3. Write the visible code. Type a short procedure such as:
    Sub ShowSheet()
        Worksheets("Sheet2").Visible = xlSheetVisible
    End Sub
    Replace "Sheet2" with the actual tab name or use the sheet's code name (for example Sheet2 without quotes) if you prefer. The code name is the name shown in the Project pane, not the tab name.
  4. Run the macro. Place the cursor inside the procedure and press F5, or choose Run > Run Sub/UserForm. Switch back to Excel (Alt+F11 again) and confirm the tab is now visible.
  5. Hide a sheet with the same property. Change the value to xlSheetHidden to hide the sheet normally, or xlSheetVeryHidden to hide it from the right-click menu in Excel. Very hidden sheets can only be made visible again through VBA or the Properties window in the editor.
  6. Loop through all sheets if needed. To unhide every sheet in the workbook, use:
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        ws.Visible = xlSheetVisible
    Next ws
    This is useful after a workbook has been locked down and you need to restore normal view.
  7. Save as macro-enabled. Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). A plain .xlsx file drops the macro when saved.
  8. Test the reverse. Run a hide macro, then a show macro, to confirm both directions work. If the workbook is protected, unprotect it first (Review > Unprotect Sheet) because protection can block visibility changes.

Common Mistakes

  • Using the tab name when the code name is needed. Worksheets("Sheet2") uses the tab name; Sheet2 without quotes uses the code name. Mixing them up causes a subscript out of range error.
  • Trying to hide the last visible sheet. Excel requires at least one visible sheet per workbook. Hiding the only visible sheet raises run-time error 1004.
  • Forgetting workbook protection. A protected workbook can block visibility changes until you unprotect it.
  • Saving as .xlsx. The macro disappears on save. Always use .xlsm.
  • Assuming VeryHidden is the same as Hidden. VeryHidden sheets do not appear in the Excel right-click Unhide list, so users may think the sheet is gone.
  • Running code in the wrong workbook. If you use Worksheets without a workbook reference, VBA targets the active workbook. Use ThisWorkbook.Worksheets to be explicit.

Tips

  • Use constants rather than numbers: xlSheetVisible, xlSheetHidden and xlSheetVeryHidden are readable and less error-prone than -1, 0 and 2.
  • Wrap the visibility change in On Error GoTo handling if the sheet name might change, so users see a clear message instead of a raw error dialog.
  • Check the current state first: If ws.Visible <> xlSheetVisible Then ws.Visible = xlSheetVisible avoids unnecessary work and errors.
  • VeryHidden is useful for settings or lookup sheets you do not want ordinary users to unhide, but it is not security. Anyone with VBA access can reverse it.
  • Keep a small "Show all sheets" macro in every workbook you distribute, so support is easy if a sheet gets hidden by accident.

Troubleshooting

If the macro does not work, check these in order, simplest first:

  1. Confirm macros are enabled. Excel may block macros from untrusted locations. Check the yellow security bar and the Trust Center settings; only enable macros from files you trust.
  2. Check the sheet name. A trailing space or different capitalisation in Worksheets("Sheet2") causes a subscript out of range error. Compare the name in the tab with the string in code.
  3. Unprotect the workbook. Review > Unprotect Workbook if protection is on. Note that unprotecting removes the protection; reapply it afterwards if needed.
  4. Make sure another sheet is visible. If you are hiding a sheet and error 1004 appears, unhide at least one other sheet first.
  5. Check where the code lives. Code in ThisWorkbook only runs on events; a normal macro belongs in a standard module.
  6. Restart Excel. If the editor behaves oddly after an error, close and reopen the workbook before deeper changes.

If none of that works, create a fresh workbook, paste the same macro and test there. That isolates whether the problem is the code or the original file.

Frequently Asked Questions

What is the difference between xlSheetHidden and xlSheetVeryHidden?

xlSheetHidden hides the sheet but users can unhide it from the right-click menu. xlSheetVeryHidden removes it from that menu, so it can only be restored through VBA or the Properties window.

Can I hide a worksheet without VBA?

Yes. Right-click the tab and choose Hide. To unhide, right-click any tab and choose Unhide. VeryHidden is the only state that requires VBA or the Properties window.

Why do I get run-time error 1004 when hiding a sheet?

The most common causes are trying to hide the last visible sheet or working with a protected workbook. Unhide another sheet or unprotect the workbook, then retry.

Does the Visible property work on chart sheets?

Yes. Chart sheets also have a Visible property and accept the same constants, so the same code pattern applies.

Will hiding a sheet affect formulas that reference it?

No. Hidden and very hidden sheets still calculate and can be referenced by formulas. Visibility only affects the interface, not the data.

Can I make a sheet visible automatically when the workbook opens?

Yes. Put the visibility code in the Workbook_Open event in ThisWorkbook. It runs each time the workbook opens, provided macros are enabled.