Excel VBA ActiveWindow.Visible hiding the wrong workbook

Josh picture Josh · Feb 3, 2014 · Viewed 34k times · Source

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

Answer

dave picture dave · Jul 25, 2015

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