VBA & Macro Problems

How to Automatically Hide Excel Worksheets by Tab Color Using VBA

Jason MillerJason Miller Nov 3, 2026 868 views

Question details

The user wants to use VBA to automatically hide specific worksheets based on their tab colors whenever the workbook is saved or closed.

How to Automatically Hide Excel Worksheets by Tab Color with VBA
Product
Excel
Device & OS
not provided
Scenario
Automating workbook organization and security by hiding sheets marked with specific theme colors before distributing or saving the file.
Observed behavior
Worksheets matching the specified tab colors are automatically hidden, while ensuring at least one worksheet remains visible in the workbook.
Before you start

Ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) so the VBA code functions correctly, and remember that Excel requires at least one worksheet to remain visible at all times.

Solution 1

Identify the Specific Tab Color Values

Before writing the script to hide the sheets, you must find the exact numerical color value of the tabs you want to target.

Excel uses specific long numeric values for tab colors. You can easily extract this number using a simple VBA message box.

1
Open the VBA Editor

Press ALT + F11 to launch the Visual Basic for Applications Editor.

2
Insert a Module

Go to Insert > Module from the top menu.

3
Run the Color Check Sub

Paste the following code: Sub DetermineColorOfTab() Dim ColorNum As Long ColorNum = Worksheets(1).Tab.Color MsgBox ColorNum End Sub Run it to see the numerical color code of the first worksheet's tab. Note this number down.

Advanced Macros in WPS Spreadsheet

Automate Tasks Effortlessly with WPS Spreadsheet

WPS Office offers robust support for VBA macros. You can run, edit, and create complex scripts like hiding sheets by tab color exactly as you would in Microsoft Excel, providing a seamless automation experience.

  1. 1. Open Your Workbook: Launch WPS Spreadsheet and open your macro-enabled workbook.
  2. 2. Access Developer Tools: Navigate to the 'Developer' tab on the top ribbon.
  3. 3. Open VBA Editor: Click the 'Visual Basic' icon to launch the editor.
  4. 4. Apply Your Macro: Double-click 'ThisWorkbook' and paste your Workbook_BeforeSave automation script.
Fully compatible with Microsoft Excel VBA macros (.xlsm files)Free and lightweight suite for everyday office tasksClean, familiar interface requiring no learning curveBuilt-in Developer tab for easy access to VBA tools
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get a runtime error when trying to hide all worksheets?

Microsoft Excel and WPS Spreadsheet require at least one worksheet to remain visible in a workbook at all times. If your loop tries to hide the last visible sheet, the application will generate an error. You must include logic to skip hiding if only one sheet is left.

What is the difference between xlSheetHidden and xlSheetVeryHidden?

Using 'xlSheetHidden' removes the sheet from view, but users can still right-click the tab bar and select 'Unhide' to reveal it. Using 'xlSheetVeryHidden' completely hides the sheet from the graphical interface; it can only be unhidden by running another VBA script.

Will theme color changes affect my macro?

Yes. If the underlying color value of a theme color is modified, the Tab.Color value may no longer match the array in your script. For reliable automation, it is recommended to test your colors thoroughly or use exact RGB values.

How do I unhide a sheet that was made 'VeryHidden'?

You must use VBA to unhide it. Open the Visual Basic Editor, select the specific sheet in the Project Explorer, open the Properties window (F4), and change the 'Visible' property to '-1 - xlSheetVisible', or run a simple macro setting 'ws.Visible = xlSheetVisible'.