In Excel VBA, a worksheet’s visibility is controlled by its Visible property. Set it to xlSheetVisible to show a worksheet, xlSheetHidden to hide it with Excel’s normal Unhide command, or xlSheetVeryHidden to prevent users from unhiding it through the worksheet-tab menu.
What the Visible property means
Visible is a property of an Excel worksheet object, not a cell, column, or field in a worksheet grid. It tells Excel whether a particular sheet is shown among the workbook’s tabs. In VBA, you can read the property to check a sheet’s current state or assign a different value to change that state.
A worksheet can be referenced through a workbook’s Worksheets collection. For example, ThisWorkbook.Worksheets("Report") refers to the sheet named Report in the workbook containing the code. The name in quotation marks is the sheet’s tab name; it is not necessarily the same as the worksheet’s VBA code name, which appears separately in the Visual Basic Editor.
For a worksheet object, VBA represents the setting with the XlSheetVisibility enumeration. Using its named constants is clearer and safer than assigning the underlying numeric values directly. The property is also available for other sheet types in the broader Sheets collection, but Worksheets specifically refers to worksheets rather than chart sheets.
The three worksheet visibility states
xlSheetVisible: The worksheet is shown as a tab and can be selected normally.xlSheetHidden: The worksheet is hidden, but a user can normally restore it by right-clicking a sheet tab and choosing Unhide.xlSheetVeryHidden: The worksheet is hidden and does not appear in Excel’s Unhide dialog. A user must change the property in the Visual Basic Editor or run VBA that makes it visible.
VeryHidden is a convenience for managing the workbook interface, not a security boundary. It can discourage accidental changes to a support sheet or lookup data, but it does not encrypt the sheet or guarantee that a determined user cannot access its contents. Use workbook passwords and appropriate file permissions for protection needs, and do not treat hidden data as confidential.
How to check a worksheet’s visibility in VBA
Read the property and compare it with the named constants. This small procedure reports whether the Report worksheet is visible, normally hidden, or very hidden:
Sub CheckReportVisibility()
Dim ws As Worksheet
' Refer to the worksheet in the workbook containing this code.
Set ws = ThisWorkbook.Worksheets("Report")
Select Case ws.Visible
Case xlSheetVisible
MsgBox "Report is visible."
Case xlSheetHidden
MsgBox "Report is hidden."
Case xlSheetVeryHidden
MsgBox "Report is very hidden."
End Select
End Sub
The code uses ThisWorkbook, which means the workbook where the macro is stored. That is often preferable to ActiveWorkbook, which means whichever workbook currently has focus and could be a different file. If your macro is stored in Personal.xlsb or an add-in and should target the workbook currently being edited, choose the target explicitly instead of assuming the two references mean the same thing.
If you only need a Boolean test for a visible sheet, compare the property directly: ws.Visible = xlSheetVisible. That expression is True only when the sheet is visible. It does not distinguish between the two hidden states, so use a Select Case statement when the distinction matters.
How to show, hide, or make a sheet very hidden
Assign the desired constant to the property. The following procedure makes Report visible and activates it afterward:
Sub ShowReportSheet()
Dim ws As Worksheet
' Get the worksheet from the workbook containing this macro.
Set ws = ThisWorkbook.Worksheets("Report")
ws.Visible = xlSheetVisible
ws.Activate
End Sub
To hide that sheet from the normal tab bar, change the assignment to ws.Visible = xlSheetHidden. To make it very hidden, use ws.Visible = xlSheetVeryHidden. The value can also be selected in the Visual Basic Editor: open the Project Explorer, select the worksheet, open the Properties window, and choose the appropriate value for Visible. The Properties window is commonly opened with F4 on Windows.
Save a copy of the workbook before running a macro that changes sheets or workbook structure. VBA actions generally cannot be reversed with Ctrl+Z, and hiding a sheet may make it less obvious how to restore it if you lose track of which sheet was changed.
Show every worksheet or manage several sheets
When a workbook has many hidden worksheets, a loop can make all of its worksheets visible. This affects every worksheet in the workbook, so use it only when that is the intended result:
Sub ShowAllWorksheets()
Dim ws As Worksheet
' Reveal every worksheet in the workbook containing this code.
For Each ws In ThisWorkbook.Worksheets
ws.Visible = xlSheetVisible
Next ws
End Sub
Conversely, a loop that hides sheets should preserve at least one visible worksheet. Excel requires a visible worksheet in a workbook; trying to hide the last visible worksheet can produce an error. A safer design first identifies which sheets should remain available, then changes only the others. It is also wise to check that the sheet names expected by a macro exist before changing them, particularly when the workbook is edited by several people.
Remember that ThisWorkbook.Worksheets includes all worksheets in the macro’s workbook, not just the active sheet or the sheet from which a button was clicked. If you intend to process the current workbook instead, use an explicit workbook reference or carefully choose ActiveWorkbook. This distinction helps prevent a macro from changing the wrong file.
VBA property versus Excel’s sheet-tab commands
For a one-time change, VBA may not be necessary. In desktop Excel, right-click the worksheet tab and choose Hide. To restore a normally hidden worksheet, right-click a visible tab, choose Unhide, select the sheet, and confirm. These commands correspond to the ordinary hidden state; they do not provide a direct way to select VeryHidden from the tab menu.
The VBA property is useful when the state needs to be changed repeatedly, controlled by a macro, or applied across multiple sheets. For example, a workbook might show a data-entry sheet to users while keeping a calculation sheet out of the way. A macro could make the calculation sheet visible during maintenance and hide it again afterward. The sheet still exists in the workbook in either hidden state, so formulas and VBA can continue to refer to it.
Common errors and practical safeguards
- “Subscript out of range”: The workbook does not contain a worksheet with the exact tab name used in the code, or the reference points to the wrong workbook. Check spelling, spaces, and the workbook reference.
- Trying to hide the last visible sheet: Leave at least one worksheet visible before hiding another. Decide which sheet should serve as the workbook’s visible starting point.
- Confusing a code name and tab name:
Worksheets("Report")uses the visible tab name. The code name shown in the Project Explorer is a different way to refer to the object and is not placed inside that string. - Expecting VeryHidden to secure data: It only changes how the sheet is exposed in Excel’s interface. Do not store sensitive information in a workbook on the assumption that hiding the sheet protects it.
- Macros do not run: The file may not be macro-enabled, or Excel’s security settings may block the code. Use a macro-enabled workbook such as
.xlsmwhen appropriate, and enable macros only for files you trust.
Workbook structure protection can also restrict changes to sheet visibility. If Excel refuses a change, check whether the workbook structure is protected and whether you are authorized to alter it. Do not work around protection unless you have permission; ask the workbook owner for the correct access or an approved copy.
Windows, Mac, and Excel for the web
VBA can be used in desktop Excel for Windows and Mac, although the editor interface and some keyboard shortcuts differ. On Windows, Alt+F11 opens the Visual Basic Editor in many Excel setups. On Mac, the corresponding shortcut is generally Option+F11; depending on the keyboard and function-key settings, you may need Fn+Option+F11. You can also use Excel’s Developer tab to open the Visual Basic Editor. If the Developer tab is not displayed, enable it in Excel’s ribbon preferences or options.
Excel for the web does not create, edit, or run VBA macros. Open the workbook in desktop Excel to use the Visual Basic Editor or execute VBA. A sheet’s visibility can still affect what users see, but a VBA solution must run in a supported desktop version. Macros also require a file format and security settings that permit them.
Frequently Asked Questions
What is the VBA code to make a worksheet visible?
Reference the worksheet and assign xlSheetVisible, such as ThisWorkbook.Worksheets("Report").Visible = xlSheetVisible. The name must match the worksheet tab name.
What is the difference between Hidden and VeryHidden in Excel VBA?
Both remove a sheet tab from view. A normally hidden sheet can be restored through Excel’s Unhide command; a VeryHidden sheet is omitted from that dialog and must be made visible through VBA or the Visual Basic Editor.
How can I tell whether a worksheet is visible?
Read its Visible property and compare it with xlSheetVisible. To tell which kind of hidden state applies, compare the property with both xlSheetHidden and xlSheetVeryHidden.
Can VBA hide every worksheet in a workbook?
No. A workbook must retain a visible worksheet. A macro should leave at least one worksheet visible, and it should handle errors if a requested visibility change is not allowed.
Does VeryHidden protect worksheet contents?
No. VeryHidden changes how the sheet appears in Excel, not the confidentiality of its contents. Treat it as an interface-management setting, not as a substitute for appropriate security.
Related
- Excel Turn Off Page Break Lines on Screen
- Excel Page Break Dotted Lines: How to Turn Them Off
- Excel: Turn Off Page Break Preview and Restore Normal View
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel UserForm Command Button: Setup and VBA GuideAn Excel UserForm command button is a clickable control that runs VBA when the user selects it. Add…
- Google Sheets Macros: How They Work and What to KnowYes. Google Sheets supports macros: you can record repeated actions and run them again, or edit the generated…
- Excel VBA Copy and Paste a Range to Another WorkbookTo copy a range from one open Excel workbook to another with VBA, use sourceRange.Copy Destination:=targetSheet.Range("B2"), replacing the…
- Excel VBA: Stop Copy and Paste ModeTo stop Excel’s moving copy border and exit copy or cut mode in VBA, set Application.CutCopyMode to False.…
- Excel VBA Get Current Workbook Name: Code and TipsTo get the name of the workbook currently active in Excel VBA, use ActiveWorkbook.Name. For example, assign Set…
- VBA Copy and Paste a File: Practical Code and Safe StepsTo copy a file with VBA in Excel for Microsoft 365 on Windows, use the FileCopy statement or…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.