Home » excel » Excel VBA ActiveWindow.Visible hiding the wrong workbook

Excel VBA ActiveWindow.Visible hiding the wrong workbook

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want to open a workbook (WB1) and then as it’s opened, another workbook (WB2) is opened. I want WB2 hidden.

Private Sub Workbook_Open()

Application.ScreenUpdating = False
Workbooks.Open Filename:="C:\WB2.xlsm"
ActiveWindow.Visible = False

End Sub

This is what I have so far and what it does is hide BOTH workbooks. I want WB1 to remain on top and visible.

Thank you! Josh

How to&Answers:

an important part would seem to be how to turn view back on again.
other post is the answer.. i just had to see it work before i commited it.
hope this is enough to explain it, may have been done in less space. thanks.

i would have to agree to post a couple words describing the key working line. i am just novice at vb & have to say that 99% of posts require some research to get a needed variable in there. i believe so enough to add some expletives as hours by many can be spent, trying to find the dang answer, but will refrain : ).

the consequence is: everyone on the planet has to spend 2 to infinite hours.
(thanks for having code, to put code in a box, needs some tweaking for lines to include / space lines interfere?).

what i found: changing out may not work: .Visible and .Hidden
– have no idea what the 1 in windows(1) is for.

sub TEST1()    'in a module
'if want to happen when you open a wb, place in: "ThisWorkbook" module as:
'Private Sub Workbook_Open()    


    Dim wb As Workbook
    'Set wb = Workbooks("WB2.xlsm")      'YES
    'Set wb = Workbooks(Filename:="C:\WB2.xlsm")    'untried should work for path eg
    'Set wb = Workbooks.Open(Filename:="C:\WB2.xlsm")   'original, with a command added: open
    Application.ScreenUpdating = False

    If 0 = 0 Then   'set to: if 0 = 1 to skip test
      If wb.WINDOWS(1).Visible = False Then   'TOGGLES: press F5 or run macro button
        wb.WINDOWS(1).Visible = True
        MsgBox "Workbook is NOT Hidden" & Space(10), vbQuestion  'a good test method
      Else
        wb.WINDOWS(1).Visible = False     '<< line to use, to hide wb on open
        MsgBox "Workbook is Hidden" & Space(10), vbQuestion  'a good test method
      End If

    Else
        wb.WINDOWS(1).Visible = False     '<< line to use, to hide wb on open
    end if
End Sub

Answer:

Private Sub Workbook_Open()
    Dim wb as workbook
    Application.ScreenUpdating = False
    set wb=Workbooks.Open(Filename:="C:\WB2.xlsm")
    wb.Windows(1).Visible = False

End Sub

Answer:

This seems to be an old post but I thought I’d add in my version of the answer,as I was working on something similar.

Set m_WB = Workbooks("WB2FilePath\WB2.xlsm")
Windows(m_WB.Name).Visible = False
'Do work
'Set WorkBook to visible
Windows(m_WB.Name).Visible = True

Keep in mind, it would be good practice to set WB2 to visible once completed. This would avoid any memory being eaten up in the background without you knowing!

Cheers.