简体   繁体   English

Excel:如果我在同一个工作簿上打开两个窗口,为什么宏在第一个窗口中停止工作?

[英]Excel: If I open two windows on the same workbook, why do macros stop working in the first window?

I am trying to set up a button in Excel to show a dual view of two separate worksheets at the same time. 我试图在Excel中设置一个按钮,同时显示两个单独的工作表的双重视图。 This is the code I've written so far (see below). 这是我到目前为止编写的代码(见下文)。 So far the code seems to work. 到目前为止,代码似乎有效。 The problem is that the top window has some activex controls on the worksheet, and they seem to stop working until the bottom window is closed again. 问题是顶部窗口在工作表上有一些activex控件,它们似乎停止工作,直到底部窗口再次关闭。 Why is this happening, and what can I do to fix it? 为什么会发生这种情况,我该怎么做才能解决这个问题? Thanks. 谢谢。

Private Sub DualViewButton_Click()
  Dim windowToPutOnTimeline As Window

  If Windows.Count = 1 Then
    ThisWorkbook.NewWindow
    Windows.Arrange xlArrangeStyleHorizontal, True, False, False
    Set windowToPutOnTimeline = Windows(1)
    If Windows(1).Top < Windows(2).Top Then
      Set windowToPutOnTimeline = Windows(2)
    End If

    With windowToPutOnTimeline
      .Activate
      HorizontalTimelineSheet.Activate
      .DisplayGridlines = False
      .DisplayRuler = False
      .DisplayHeadings = False
      .DisplayWorkbookTabs = False
      '.EnableResize = False
    End With

    Windows(2).Activate 'go back to the right focus the user expects.

  Else
    If Windows(1).Top = Windows(2).Top Then
      Windows.Arrange xlArrangeStyleHorizontal, True, False, False
    Else
      Windows.Arrange xlArrangeStyleVertical, True, False, False
    End If
  End If
End Sub

EDIT: if I switch the window that's being assigned to windowToPutOnTimeline then the problem goes away. 编辑:如果我切换分配给windowToPutOnTimeline的窗口,那么问题就会消失。 So I've essentially worked around the problem without knowing why it works the other way. 所以我基本上解决了这个问题而不知道它为什么会以另一种方式工作。 (see code snippet below) (见下面的代码片段)

With ThisWorkbook
  Set windowToPutOnTimeline = .Windows(1)
  Set windowToPutOnDataSheet = .Windows(2)
  tmp = .Windows(1).Top
  .Windows(1).Top = .Windows(2).Top
  .Windows(2).Top = tmp
End With

This behavior is a bug in ActiveX control. 此行为是ActiveX控件中的错误。

As a work around, use a button from the Forms Controls, rather than an ActiveX button 作为解决方法,使用Forms Controls中的按钮,而不是ActiveX按钮

Using the Forms button you will need to add a Module, declare a Sub with your code and assign the Sub as the action macro to your button (as apposed to putting your code in the click event of an ActiveX button) 使用“表单”按钮,您需要添加一个模块,使用您的代码声明一个Sub,并将Sub作为操作宏指定给您的按钮(因为将代码放入ActiveX按钮的单击事件中)

I tried this on Excel 2007, seem to work OK - the button appears and works on both windows 我在Excel 2007上尝试了这个,似乎工作正常 - 按钮出现并在两个窗口都有效

暂无
暂无

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

相关问题 我保存了一个 excel xlsm 工作簿,其中有 2 个同名的宏 - 现在我无法打开文件 - I saved an excel xlsm workbook that had 2 macros with the same name - now I can't open the file Excel VBA:如何在不运行宏的情况下打开Excel文件,但希望在激活工作簿时运行宏? - Excel VBA: How do I open an Excel file without running macro but want macros to run when I activate workbook? 如何在 PowerShell 中禁用宏的情况下打开 Excel 工作簿 - How to open an Excel Workbook with Macros Disabled in PowerShell 如何在自动打开时停止Excel工作簿闪烁? - How can I stop Excel workbook flicker on automation open? Excel:你能在两个窗口中打开一个工作簿吗? - Excel: Can you open a single workbook in two windows? 在excel窗口中打开附件并复制以打开工作簿 - Open attachment in excel window and copy to open workbook SQL 打开 Excel 工作簿时 VBA 宏中的查询 - 缓存问题 - SQL Queries in VBA Macros on Open Excel Workbook - Caching Issue Excel中的“工作表更改”事件仅在我打开工作簿第一次时有效。 之后对单元格所做的任何更改均不起作用 - Worksheet Change event in excel only works the first time i Open the workbook. any changes to cell after do not work 如何在Excel启动时运行代码(不是workbook_open)? - How do I run code on Excel startup (not workbook_open)? 有没有办法用宏运行同一个excel工作簿的多个实例? - Any way to run multiple instances of the same excel workbook with macros?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM