Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code to hide only one workbook instead of hiding all currently open workbooks

Tags:

excel

vba

I usually have multiple workbooks open and on one workbook, I have a VBA code that hides the workbook and shows a UserForm. But when I open that workbook, all of my workbooks that are currently opened will also hide. What code can I use to just hide 1 workbook?

Here are the codes I've tried:

ThisWorkbook.Application.Visible = False

Windows(ThisWorkbook.name).Visible = False

Application.Windows(1).visible = false

With Windows(ThisWorkBook.name).visible = False works with closing only one workbook, it messes with the workbook and the sheets don't show at all. I can't even close the excel workbook without using the task manager.

like image 325
Hush Avatar asked Mar 20 '23 15:03

Hush


1 Answers

ThisWorkbook.Application.Visible = False will change the Visible property of the application running your workbook, i.e. an instance of Excel.EXE ... if this instance is running your other books, too, then as a consequence all these books will disappear from screen.

To hide a single workbook, use

ActiveWindow.Visible = False

or alternatively, if the workbook you want to hide (e.g. "MyWorkbook") is not the active one

Windows("MyWorkbook").Visible = False

Pay attention that hiding a window also moves the pointer to the ActiveSheet, likewise when you reverse this (i.e. ...Visible = True) the displayed sheet becomes active.

like image 98
MikeD Avatar answered May 03 '23 20:05

MikeD