Hide or Show Excel Ribbon, Formula Bar, Gridlines, and Headings with VBA
Question details
Use Excel workbook events to control the visibility of the Ribbon, Formula Bar, gridlines, and row and column headings.

- 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.
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.
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.
Open the workbook in Excel, press Alt+F11, and select ThisWorkbook under the workbook's project in the VBA editor.
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
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.

Change Excel view settings manually
Use Excel's interface controls when you do not need the changes to run automatically.
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.

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.




