[英]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.