To hide the active workbook window with VBA, set its Visible property to False, for example: ThisWorkbook.Windows(1).Visible = False. This hides the workbook from view, but does not close, encrypt, or secure the file; you can restore it with Excel’s Unhide command or another macro.
What hiding a workbook means
In Excel, a workbook is the file and a window is the on-screen view of that file. Hiding a workbook window removes that view from the workspace while leaving the workbook open in the current Excel session. Its data and VBA project remain loaded, and a macro can still refer to the workbook.
This distinction matters because “hide a workbook” is sometimes used to describe several different actions. Hiding a window is not the same as hiding a worksheet, minimizing Excel, closing the file, or protecting information. A hidden workbook may still be discoverable and can generally be made visible again by someone with access to the open Excel session.
Hide a workbook window with a macro
For a workbook with one window, the core instruction is ThisWorkbook.Windows(1).Visible = False. ThisWorkbook means the workbook containing the running VBA code. The Windows(1) reference selects its first window, and setting Visible to False hides that window.
To add a reusable macro in Excel for Microsoft 365 on Windows, open the workbook, choose Developer > Visual Basic, and insert a standard module with Insert > Module. Paste in the code below. If the Developer tab is not shown, turn it on through File > Options > Customize Ribbon and select Developer.
Option Explicit
Sub HideThisWorkbook()
Dim wbWindow As Window
' Hide every window belonging to the workbook that contains this code.
For Each wbWindow In ThisWorkbook.Windows
wbWindow.Visible = False
Next wbWindow
End Sub
Sub ShowThisWorkbook()
Dim wbWindow As Window
' Make every window belonging to this workbook visible again.
For Each wbWindow In ThisWorkbook.Windows
wbWindow.Visible = True
Next wbWindow
End Sub
Before running a macro, save a copy of the workbook. VBA changes cannot be undone with Ctrl+Z. To run the hide macro, return to Excel and choose Developer > Macros, select HideThisWorkbook, then choose Run. You can also press Alt+F8 on Windows to open the Macro dialog. Save the file as an Excel Macro-Enabled Workbook (.xlsm) if you need to retain its VBA code.
How to restore a hidden workbook
The usual way to bring a hidden window back is Excel’s Unhide command. In the Windows desktop ribbon, choose View > Unhide, select the workbook in the list, and choose OK. If the workbook has several hidden windows, Excel may let you restore them individually; repeat the command as needed.
You can also run the ShowThisWorkbook procedure above, provided you can run that macro. For example, it may be available in the Macro dialog, or you can run it from a separate visible workbook if the code is stored there and refers to the hidden workbook. If the macro is stored only in a hidden workbook, the ribbon’s Unhide command is the simpler recovery method. Avoid closing Excel before restoring the window if you are unsure whether you can reopen the file easily.
When a workbook has more than one window
Excel can display multiple windows for the same workbook. The command View > New Window creates another view, often with a suffix such as :1 or :2 in the title bar. In that situation, hiding only Windows(1) may leave another window visible. The loop in the sample macro visits every window in ThisWorkbook.Windows, so it hides or shows all of them.
If your intention is to hide just one view and leave another open, select that window and hide only the active window instead. The VBA reference to the active window is different from a reference to every window in a workbook, so be deliberate about which behavior you want. For ordinary single-window workbooks, the loop is a convenient way to avoid accidentally leaving a second view on screen.
Hide a window, hide a sheet, or protect a file?
Choose the action that matches your goal. These features solve different problems:
- Hide a workbook window: Removes the workbook’s view from the current Excel workspace. It is useful for keeping a data or support workbook out of the way while another workbook is in use.
- Hide a worksheet: Hides one sheet while leaving the workbook window visible. Use the sheet tab’s Hide command, or set a worksheet’s
Visibleproperty in VBA. A sheet markedxlSheetVeryHiddenis not listed in the ordinary Unhide Sheets dialog, but a user with suitable VBA access can still change its visibility. - Protect a workbook or worksheet: Helps restrict particular changes, such as editing cells or changing workbook structure. Protection is not a substitute for strong file encryption and does not make confidential data safe from someone who can access the file.
- Close or encrypt the file: Closing removes it from the open session. File encryption, configured when saving, is the appropriate Excel feature when the goal is to require a password to open a file. Keep any password somewhere safe.
A common misconception is that a hidden workbook is private. Hiding is primarily a display and workflow convenience. It should not be used to conceal sensitive records, bypass access controls, or promise that other users cannot inspect the workbook.
Common problems and how to fix them
- The macro runs but Excel still shows the workbook: Check whether the workbook has another visible window. The sample loop hides all windows in the workbook; a one-line instruction targeting only
Windows(1)does not. - Unhide is unavailable or the workbook is not listed: Confirm that the workbook is open in the same Excel instance and that its window was hidden rather than closed. If it has already been closed, reopen the file instead.
- The macro does not appear or will not run: Confirm that the code is in a standard module and that the file was saved as
.xlsm. Excel’s macro security settings may also block it. Only enable macros in files you trust; do not weaken global security settings just to run an unfamiliar file. - Excel looks empty after hiding the workbook: The workbook window may be hidden while Excel itself remains open. Use View > Unhide to restore it. If other workbooks remain open, their windows should still be available.
If a macro hides the window before the workbook is saved, the window state may be retained when the file is next opened, depending on how the workbook is saved and its state at that time. Test the workflow with a copy rather than relying on a hidden file as a permanent setting. A workbook that opens without an obvious window can confuse its next user.
Windows, Mac, and Excel for the web
The VBA approach is for desktop Excel. Excel for the web can open many macro-enabled workbooks, but it does not run VBA macros; open the file in the desktop app to use or edit this code. In Excel for Mac, VBA is available in the desktop application, though ribbon labels and window-management menus can differ from Windows. The Developer tab may need to be enabled in Excel settings.
On Windows, Alt+F11 opens the Visual Basic Editor, and Alt+F8 opens the Macro dialog. On Mac, the Visual Basic Editor shortcut is commonly Option+F11, sometimes with the Fn key depending on the keyboard configuration; check Excel’s menu or shortcut settings if it does not work. Do not assume Windows ribbon paths or shortcuts are identical on Mac.
Frequently Asked Questions
Can VBA hide the workbook that contains the macro?
Yes. Use ThisWorkbook.Windows(1).Visible = False for its first window, or loop through ThisWorkbook.Windows to hide all its windows. The workbook stays open.
Can I hide a workbook without closing it?
Yes. Hiding its window leaves it open in Excel. Use View > Unhide to display it again, or run a macro that sets its window’s Visible property to True.
Does hiding a workbook protect its data?
No. It only hides a window; it does not encrypt the file or prevent someone with access to Excel from restoring the window. Use appropriate file security for sensitive information.
Can I hide a workbook in Excel for the web?
Excel for the web does not run VBA, so this macro cannot be run there. Use the desktop version of Excel for VBA and its workbook-window controls.
Why does one workbook window remain visible?
The workbook may have multiple windows. A macro that changes only Windows(1) affects just that window; loop through the workbook’s Windows collection to hide each one.
Related
- Excel Split Cell Contents by Delimiter
- Excel Split Cell Based on Character: Methods That Work
- Excel Split Cell at a Specific Character?
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Outlook VBA: Select an Excel Worksheet by PathIn Outlook VBA on Windows, create an Excel application object, open the workbook by its full file path,…
- VBA Add Worksheet After the Last SheetTo add a worksheet after the last sheet tab in Excel, use Worksheets.Add with the last item in…
- VBA Convert to String: How CStr WorksIn VBA, use the built-in CStr function to convert a value to a string: for example, CStr(42) returns…
- Dim ws As Worksheet in VBA: Meaning and UseDim ws As Worksheet declares a variable named ws that can refer to an Excel worksheet, but it…
- Excel VBA INDEX: Return Values from a RangeIn Excel VBA, “INDEX” usually means Excel’s INDEX worksheet function, called as Application.WorksheetFunction.Index(...) or Application.Index(...) to return an…
- Excel VBA Get a Character from a String by PositionTo get one character from a string in Excel VBA, use Mid$(text, position, 1); the position starts at…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.