以并排模式查找窗口

clu*_*man 5 excel vba excel-vba

The "View Side By Side" and "Synchronous Scrolling" made the comparison of 2 spreadsheets easier than ever. However, there isn't a "Synchronous Switching tab" feature, so if I switch to a different tab in one of the workbooks and continue scrolling, the sync'ed scrolling become quite funny.

Well, I shouldn't complain, because it's all done manually, and I should use this feature wisely.

As a lazy developer, I would like to write some code to dig myself out: Can I write a macro to automate the worksheet switching on the peer window in side-by-side mode?

It breaks down to 2 steps:

  1. how do I know if a window, most likely the ActiveWindow, is in side-by-side mode?
  2. if it is, how do I tell which window is its peer?

我做了我的功课.看来Excel对此功能的编程不是很友好.有3种方法

  • BreakSideBySide()
  • CompareSideBySideWith(WindowName)
  • ResetPositionsSideBySide()

和1个布尔属性

  • SyncScrollingSideBySide

关于Windows此功能的集合,但不足以解决我的问题.

有谁知道如何实现这一目标?或者,这确实不可能吗?先感谢您.

mr.*_*and 0

您可以使用 Workbook_SheetActivate 事件来实现此目的:

http://msdn.microsoft.com/en-us/library/office/ff195710.aspx

如果您将此代码放入 ThisWorkbook 对象中,那么每次更改活动工作表时,它都会...

  1. 循环浏览所有打开的具有不同名称的工作簿
  2. 查找与您刚刚单击的工作表同名的工作表
  3. 激活其他工作簿中的工作表

    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    
        For i = 1 To Application.Workbooks.Count
            If Application.Workbooks(i).Name <> ThisWorkbook.Name Then
    
                Dim otherWB As Workbook
                Set otherWB = Application.Workbooks(i)
                otherWB.Sheets(Sh.Name).Activate
    
            End If
        Next
    
    End Sub
    
    Run Code Online (Sandbox Code Playgroud)

请注意,这要求工作表存在于所有打开的工作簿中。如果不这样做,就会产生错误。但是,您可以轻松添加错误处理来忽略未找到相应工作表的工作簿。

另请注意,当仅打开两个工作簿时,最好使用此功能。我没有研究您提到的其他方法,但可能存在一种方法来识别当前处于并排模式的两个工作簿,此时代码可以摆脱 for 循环并变得更加简洁。