简体   繁体   English

ActiveWindow.NewWindow Windows("aFile.xlsm:1").Activate 不适用于 Excel 365

[英]ActiveWindow.NewWindow Windows("aFile.xlsm:1").Activate isn't working on Excel 365

Here was my original Question,这是我原来的问题,

How to check if "afile.xlsm:2" is already open VBA 如何检查“afile.xlsm:2”是否已经打开 VBA

I am building my Workbook on Excel 2013 and the above solution works.我正在 Excel 2013 上构建我的工作簿,上述解决方案有效。 The Office has upgraded to 'Office 365'. Office 已升级到“Office 365”。 I noticed after 'Excel 365' Opens the new window, it calls the open windows "aFile.xlsm - 1" & "aFile.xlsm - 2" compared to "aFile.xlsm:1" & "aFile.xlsm:2"我注意到在“Excel 365”打开新窗口后,与“aFile.xlsm:1”和“aFile.xlsm:2”相比,它调用打开的窗口“aFile.xlsm - 1”和“aFile.xlsm - 2”

Since my Debugger is stating "Run-time error '9' Subscript out of range" on line由于我的调试器在线指出“运行时错误‘9’下标超出范围”

Windows("aFile.xlsm:1").Activate

, I tried changing my VBA Code to recognize "aFile.xlsm - 1" & "aFile.xlsm - 2" but to no prevail. ,我尝试更改我的 VBA 代码以识别“aFile.xlsm - 1”和“aFile.xlsm - 2”,但没有成功。

Function AlreadyOpen(sFname As String) As Boolean
    Dim wkb As Workbook
    'Dim sFname As String
    sFname = "aFile.xlsm:2"
    On Error Resume Next
    Set wkb = Workbooks(sFname)
    AlreadyOpen = Not wkb Is Nothing
    Set wkb = Nothing
End Function

...omitted, what I think is unnecessary code related to this question. ...省略,我认为是与此问题相关的不必要代码。

Dim sFilename As String

sFilename = "aFile.xlsm:2"

If AlreadyOpen(sFilename) Then
    Sheets("Sheet2").ListObjects("Table24").Range.AutoFilter Field:=5, Criteria1:=SearchString
Else
    If myButton.Text = "SITE" Then
    Sheets("Sheet1").Select
    ActiveWindow.NewWindow
    Windows("aFile.xlsm:1").Activate
    Windows("aFile.xlsm:2").Activate
    Windows.Arrange ArrangeStyle:=xlVertical
    Sheets("Sheet2").Select
    ActiveWindow.Zoom = 55

    ActiveSheet.ListObjects("Table24").Range.AutoFilter Field:=5, Criteria1:=SearchString
    End If
End If
Exit Sub

End Sub

How can I have this Code work on Excel 2013 and Excel 365?如何让此代码在 Excel 2013 和 Excel 365 上运行? I'd rather not compile;我宁愿不编译;

computername = Environ("computer name") 'Get computer name
username = Environ("user name") 'Get user name 

into if statements.进入 if 语句。

It might be worth your while to run this while you have both windows open当你打开两个窗口时运行它可能是值得的

Dim w As Window
For Each w In Application.Windows
    Debug.Print w.Caption
Next w

And then simply copy/paste the results from your Immediate Window into the appropriate areas of your vba code, as there may be characters you can't discern in the window title.然后只需将即时窗口中的结果复制/粘贴到 vba 代码的适当区域,因为窗口标题中可能存在您无法识别的字符。

Additionally, if you wanted to go a more dynamic route, you could do something like此外,如果你想走一条更动态的路线,你可以做类似的事情

Dim w As Window
For Each w In Application.Windows
    If w.Caption LIKE "*aFile*2*" Then '<- the same would be used for "*aFile*1*"
        w.Activate
        Exit For
    End if
Next w

Would work as well.也会起作用。

In O365, the Caption has changed from ":1" to " - 1"在 O365 中,标题已从“:1”更改为“-1”

You can ID this by Debug.Print Activewindow.caption您可以通过 Debug.Print Activewindow.caption 对此进行标识

You'll notice there are (2) spaces before and after the "-"您会注意到“-”前后有 (2) 个空格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM