Excel VBA ActiveWindow.Visible 隐藏了错误的工作簿

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21538320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:41:34  来源:igfitidea点击:

Excel VBA ActiveWindow.Visible hiding the wrong workbook

excelvbavisibleactive-window

提问by Josh

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

我想打开一个工作簿 (WB1),然后在打开它时打开另一个工作簿 (WB2)。我想隐藏 WB2。

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.

这是我到目前为止所拥有的,它的作用是隐藏两个工作簿。我希望 WB1 保持在顶部并可见。

Thank you! Josh

谢谢!乔希

回答by dave

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 : ).

我不得不同意发表一些描述关键工作路线的词。我只是 vb 的新手,不得不说 99% 的帖子都需要进行一些研究才能在其中获得所需的变量。我相信这足以添加一些咒骂,因为许多人可以花费几个小时,试图找到该死的答案,但会避免:)。

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?).

结果是:地球上的每个人都必须花费 2 到无限个小时。
(感谢您提供代码,将代码放入框中,需要对行进行一些调整以包含/空格行干扰?)。

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

我发现:改变可能不起作用:.Visible 和 .Hidden - 不知道 windows(1) 中的 1 是做什么用的。

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

回答by Tim Williams

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

回答by Dak Narendra

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!

请记住,完成后将 WB2 设置为可见是一种很好的做法。这将避免在您不知情的情况下在后台吃掉任何内存!

Cheers.

干杯。