Excel VBA Workbook Visible: Step-by-Step Guide
In this article
To make a workbook visible in Excel VBA, set its Visible property to True (or xlSheetVisible for a worksheet). The direct answer: use Workbooks("Book1.xlsx").Windows(1).Visible = True for a workbook window, or ThisWorkbook.Windows(1).Visible = True for the workbook containing the code. If the workbook itself is hidden, you must first unhide it via the View tab or by setting Workbooks("Name").Windows(1).Visible = True from another workbook. The steps below cover both scenarios, plus common pitfalls and troubleshooting.
What You'll Need
- Microsoft Excel (any modern version) with the Developer tab enabled.
- Basic familiarity with the VBA editor (Alt+F11).
- Macro security settings that allow macros to run (you may need to enable macros for the file).
- If the workbook is password-protected, you'll need the password to unhide it.
Step-by-Step
- Open the VBA editor. Press Alt+F11 (Windows) or Fn+Alt+F11 (Mac). If the Developer tab is not visible, enable it via File > Options > Customize Ribbon and check Developer.
- Identify the workbook name. In the Project Explorer (usually top-left), find the workbook you want to make visible. Its name appears as VBAProject (Book1.xlsx). Note the exact filename including extension; you'll need it in code.
- Insert a new module. In the VBA editor, right-click your project, choose Insert > Module. This is where you'll write the code.
- Write the basic unhide code. For a workbook that is hidden (not just its window), use:
Sub ShowWorkbook()
Workbooks("Book1.xlsx").Windows(1).Visible = True
End Sub
Replace "Book1.xlsx" with your actual filename. If the workbook is already open but its window is hidden, this will show it. - Handle the case where the workbook is not open. If the workbook is closed, you must open it first:
Sub OpenAndShow()
Dim wb As Workbook
Set wb = Workbooks.Open("C:\Path\To\Book1.xlsx")
wb.Windows(1).Visible = True
End Sub
Adjust the path. Note that opening a workbook may trigger macros or prompts; setApplication.DisplayAlerts = Falsetemporarily if needed, but restore it afterward. - Make a worksheet visible. If you need to unhide a worksheet (not the workbook window), use:
Worksheets("Sheet1").Visible = xlSheetVisible
This works only if the workbook itself is visible and the sheet is not protected. For very hidden sheets, usexlSheetVeryHiddento hide, andxlSheetVisibleto show. - Run the macro. Press F5 while the cursor is in the subroutine, or use Run > Run Sub/UserForm. Alternatively, assign the macro to a button on a visible sheet.
- Save and test. Save your workbook as .xlsm (macro-enabled). Close and reopen to ensure the visibility persists. Note: if you hide a workbook via VBA and save, it may remain hidden on next open; the code above will unhide it when run.
Common Mistakes
- Using
Workbooks("Book1").Visible = Trueinstead ofWindows(1).Visible. TheWorkbookobject does not have aVisibleproperty; you must use itsWindowscollection. The correct syntax isWorkbooks("Book1.xlsx").Windows(1).Visible = True. - Forgetting to open the workbook first. If the workbook is closed, referencing it by name will cause a subscript out of range error. Open it with
Workbooks.Openbefore setting visibility. - Assuming
ThisWorkbookis always visible. If the code is in an add-in or a hidden workbook,ThisWorkbook.Windows(1).Visible = Truemay fail if no window exists. CheckThisWorkbook.Windows.Countfirst. - Mixing up workbook and worksheet visibility.
Workbook.Windows(1).Visiblehides the entire Excel window for that workbook;Worksheet.Visiblehides a single sheet tab. Use the right one for your goal. - Not handling errors. If the workbook name is wrong or the file is missing, the macro stops. Add
On Error Resume Nextor check withDirbefore opening. - Leaving
DisplayAlertsoff. If you setApplication.DisplayAlerts = Falseto suppress prompts, always set it back toTruebefore exiting, or Excel may not warn you about unsaved changes.
Tips
- Use
ThisWorkbook.Windows(1).Visible = Truewhen working within the same workbook to avoid hardcoding names. - To check if a workbook is already open, loop through
Workbooksand compare names case-insensitively. - For workbooks that should never be hidden, set
ThisWorkbook.Windows(1.Visible = Truein theWorkbook_Openevent. - When hiding a workbook via VBA, consider that users may not know how to unhide it manually; provide a visible button or instructions.
- Use
xlSheetVisibleandxlSheetVeryHiddenconstants for worksheet visibility; they are more readable than numeric values. - Test macros in a copy of your workbook first, especially if they modify visibility of critical sheets.
Troubleshooting
If the macro does not make the workbook visible, work through these checks in order:
- Check the workbook name and path. A typo in the filename or an incorrect path will cause a "Subscript out of range" error. Use
Debug.Print Workbooks("Book1.xlsx").FullNameto verify. - Ensure the workbook is open. If it's closed, open it first. If it's open but hidden, the
Windows(1).Visiblemethod should work; if not, tryWorkbooks("Book1.xlsx").Activatebefore setting visibility. - Check for protected view or read-only mode. Workbooks opened from email or the internet may be in Protected View, where VBA cannot change visibility. Enable editing first.
- Verify macro security. If macros are disabled, the code won't run. Check File > Options > Trust Center > Trust Center Settings > Macro Settings.
- Look for hidden windows. If
Windows.Countis 0, the workbook has no window (common for add-ins). You may need to create a new window withWorkbooks("Book1.xlsx").Windows.Add. - Check for password protection. If the workbook structure is protected, you cannot unhide sheets or windows without the password. You'll need to unprotect it first (Review > Unprotect Workbook).
- Restart Excel. Sometimes the VBA project gets into a bad state; closing and reopening Excel can resolve odd behavior.
Frequently Asked Questions
How do I unhide a workbook that was hidden with VBA?
Open the VBA editor (Alt+F11), open the Immediate Window (Ctrl+G), and type Workbooks("Book1.xlsx").Windows(1).Visible = True then press Enter. Replace the filename with your actual workbook name. If you don't know the name, type For Each wb In Workbooks: Debug.Print wb.Name: Next to list open workbooks.
What is the difference between hiding a workbook and hiding a worksheet?
Hiding a workbook (via Windows(1).Visible = False) hides the entire Excel window for that workbook, so the user cannot see or interact with it. Hiding a worksheet (via Worksheets("Sheet1").Visible = xlSheetHidden) hides only the sheet tab; the workbook window remains visible and other sheets are accessible.
Can I make a workbook visible without using VBA?
Yes. If a workbook window is hidden, go to View > Unhide and select the workbook from the list. If the workbook itself is hidden (not just its window), you may need to use VBA or the Window menu. Note that Excel's Unhide option only lists workbooks that are open but have hidden windows.
Why do I get a "Subscript out of range" error when setting Visible?
This error means VBA cannot find the workbook by the name you provided. Check the exact filename including extension, ensure the workbook is open, and verify there are no extra spaces. Use Workbooks.Count and loop through names to confirm.
Can I hide a workbook so users cannot unhide it easily?
You can set Windows(1).Visible = False and protect the VBA project with a password (Tools > VBAProject Properties > Protection). However, this is not strong security; determined users can still unhide it via the View tab or by disabling macros. For sensitive data, use workbook protection and encryption instead.
Does making a workbook visible affect other open workbooks?
No, visibility is per workbook window. Setting one workbook's window to visible does not change others. However, if you hide the active workbook, Excel may activate another open workbook automatically.