简体   繁体   English

使用Excel 2016,当宏与工作表关联时,VBA将不会激活备用工作表

[英]With Excel 2016, VBA will not activate an alternate worksheet when the Macro is associated with a worksheet

I have a Macro that is associated with my "Introduction" sheet. 我有一个与“简介”工作表相关联的宏。 It is triggered by a cell change and then calls a second macro that manipulates another worksheet, "TimeInLibraryData". 它是由单元格更改触发的,然后调用第二个宏来操纵另一个工作表“ TimeInLibraryData”。 It's pretty simple: 很简单:

Private Sub Worksheet_Change(ByVal Target As Range) 专用子Worksheet_Change(按目标的ByVal目标)

Sheets("TimeInLibraryData").Visible = True
Sheets("TimeInLibraryData").Activate

MsgBox "The name of the active sheet is " & ActiveSheet.Name

Call CreateTimeLine.CreateTimeLine1(1)

End Sub 结束子

Public Sub CreateTimeLine1(PickSingleLib As Long) 公共子CreateTimeLine1(PickSingleLib只要很长)

Sheets("TimeInLibraryData").Activate

MsgBox "The name of the active sheet is " & ActiveSheet.Name

End Sub 结束子

You can see I am outputting the Active Sheet name. 您可以看到我正在输出活动表名称。 The problem is that in both places shown, i see that the ActiveSheet is the "Introduction" sheet when it should be "TimeInLibraryData" 问题是在所示的两个地方,我都看到ActiveSheet是“ Introduction”工作表,而应该是“ TimeInLibraryData”

The application was written in Excel 2010 and i have just updated to Excel 2016 where the problem is seen. 该应用程序是用Excel 2010编写的,而我刚刚更新到出现问题的Excel 2016。

Running in Excel 2016, if I access the CreateTimeLine1 macro during normal runtime, it works. 在Excel 2016中运行,如果我在正常运行时访问CreateTimeLine1宏,它将起作用。 I only see a problem when the Macro is called following a change to the "Introduction" worksheet. 我仅在更改“简介”工作表后调用宏时才看到问题。

I have created a cut down example in VBA 2016 and found that it works as expected. 我在VBA 2016中创建了一个简化的示例,发现它按预期工作。 I also created the simple example in Excel 2010 and ran it in Excel 2016 which also worked. 我还在Excel 2010中创建了简单示例,并在Excel 2016中运行了该示例,该示例也起作用。

So - I have a very perplexing situation tied to the running a set of Macros written in Excel/VBA 2010 that is not working correctly in Excel/VBA 2016 所以-我遇到一个非常令人困惑的情况,与运行用Excel / VBA 2010编写的一组宏在Excel / VBA 2016中无法正常运行有关

I'm using Excel 2016 with the latest patches and can't reproduce the behavior. 我正在使用具有最新修补程序的Excel 2016,无法重现此行为。 You should sandwich your call to Sheets("TimeInLibraryData").Activate between Application.EnableEvents = False and Application.EnableEvents = True , to ensure no other event handlers catch the Worksheet_Activate event your line will fire. 您应该Sheets("TimeInLibraryData").Activate调用夹在中间Sheets("TimeInLibraryData").ActivateApplication.EnableEvents = FalseApplication.EnableEvents = True之间进行Sheets("TimeInLibraryData").Activate ,以确保没有其他事件处理程序捕获您的行将触发的Worksheet_Activate事件。

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

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