VBA & Macro Problems

Hide or Show Excel Ribbon, Formula Bar, Gridlines, and Headings with VBA

Maira MehtabMaira Mehtab Nov 18, 2026 870 views

Question details

Use Excel workbook events to control the visibility of the Ribbon, Formula Bar, gridlines, and row and column headings.

Hide or Show Excel Interface Elements with Workbook Events
Product
Microsoft Excel
Device & OS
not provided
Scenario
The user wants to hide interface elements when a workbook opens and show them again before it closes.
Observed behavior
The original VBA uses an invalid Workbook_Close event and applies a window-level property at the application level.
Before you start

Test the VBA in a copy of the workbook and save it as an Excel Macro-Enabled Workbook (.xlsm). Enable macros only if you trust the file, and check that the workbook has a visible window when testing.

Solution 1Recommended

Use workbook events to control Excel interface visibility

Hide the interface elements on opening and show them before closing by placing the event procedures in ThisWorkbook.

DisplayGridlines and DisplayHeadings are window properties. DisplayFormulaBar applies to the Excel application, and the Ribbon command also affects the Excel interface beyond this workbook.

This example changes the first window of the workbook and restores all four elements to visible, rather than restoring their previous individual settings.

1
Open the workbook module

Open the workbook in Excel, press Alt+F11, and select ThisWorkbook under the workbook's project in the VBA editor.

2
Add the event procedures

Paste the following code into ThisWorkbook, combining it with any existing Workbook_Open or Workbook_BeforeClose procedure rather than creating duplicates: Private Sub Workbook_Open() If ThisWorkbook.Windows.Count > 0 Then ThisWorkbook.Windows(1).DisplayGridlines = False ThisWorkbook.Windows(1).DisplayHeadings = False End If Application.DisplayFormulaBar = False Application.ExecuteExcel4Macro "show.toolbar(""Ribbon"",False)" End Sub Private Sub Workbook_BeforeClose(Cancel As Boolean) If ThisWorkbook.Windows.Count > 0 Then ThisWorkbook.Windows(1).DisplayGridlines = True ThisWorkbook.Windows(1).DisplayHeadings = True End If Application.DisplayFormulaBar = True Application.ExecuteExcel4Macro "show.toolbar(""Ribbon"",True)" End Sub

3
Save and test the events

Use File > Save As to save the workbook as an .xlsm file. Close and reopen the trusted workbook, enable macros if prompted, and check the interface both on open and before closing.

Use workbook events to control Excel interface visibility
Check the result and save prompt: Visibility changes can mark the workbook as modified and trigger a save prompt. If closing is canceled, the BeforeClose procedure may already have shown the elements. The legacy Ribbon command may be restricted by Excel settings or policies.
Free Microsoft Office alternative

Manage Spreadsheet Views with WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. It supports Excel formats for a straightforward move between office suites, although it does not fix Excel workbook event code.

Adjust spreadsheet view options for a cleaner worksheet displayOpen and edit Microsoft Excel formats such as XLSX and XLSWork in a familiar, easy-to-use spreadsheet interfaceUse a lightweight office suite for everyday documents
microsoft office alternative - wps office

Frequently Asked Questions

Why does Workbook_Close not run in Excel?

Excel uses the Workbook_BeforeClose event instead. Put Private Sub Workbook_BeforeClose(Cancel As Boolean) in the workbook's ThisWorkbook module.

Why can't I set DisplayGridlines on the Application object?

DisplayGridlines and DisplayHeadings are properties of an Excel window. Use a window reference such as ThisWorkbook.Windows(1); use Application.DisplayFormulaBar for the Formula Bar.

Will the code restore my original display settings?

No. The example sets all four elements to visible before closing. Preserving the original settings requires storing their previous states and restoring those values.

What if the Ribbon command does not work?

The example uses a legacy Excel 4.0 macro command, which may be restricted by settings or organizational policy. Check your Excel security policy, or use Ribbon Display Options or Ctrl+F1 to control the Ribbon manually.

Why do I get a save prompt when closing the workbook?

Changing window display settings can mark a workbook as modified. Check the prompt before closing and save if you want to keep the workbook changes.