请测试附带的程序Test()。
Sub Test()
Application.ScreenUpdating = False
Workbooks.Open ThisWorkbook.Path & "\Book2.xlsm"
Application.ScreenUpdating = True
ThisWorkbook.Activate
End Sub
ThisWorkbook应该在程序结束时处于活动状态,但事实并非如此。注释掉第一行代码,ThisWorkbook在程序结束时处于活动状态
我第一次遇到问题时运行的实际代码如果ThisWorkbook.Activate不起作用将导致运行时错误,因此非常混乱。我猜这与SDI Excel 2013使用有关。
我已尝试在ThisWorkbook.activate之前将Application.ScreenUpdating设置为true,之后无效。没有来自DoEvents的帮助或等待。
作为一种解决方法,我在过程结束时使用了MsgBox,当Application.ScreenUpdating设置为false时,由于某种原因,它会激活ThisworkBook
你得到相同的结果吗?是否有更好的解决方法?
以下Sub TestWithComments()提供了其他信息。 谢谢,
Sub TestWIthComments()
'Demonstrates Workbook.Activate does not work when Application.ScreenUpdating = False
' Windows 8.1 V 6.3.9600 Build 9600
' Excel 2013 (15.0.4551.1510) MSO(15.0.4551.1508)32-bit
'Place this procedure in any workbook
' Create a workbook named Book2.xlsm in the same path as the workbook with this code
' If "Application.ScreenUpdating = False" is commented out then _
ThisWorkbook is active at the end of the procedure _
Else _
Book2.xlsm is active at the end of the procedure
Application.ScreenUpdating = False
'Open Book2
Workbooks.Open ThisWorkbook.Path & "\Book2.xlsm"
ThisWorkbook.Activate
' If "MsgBox ThisWorkbook.Name" is commented out then _
Book2.xlsm is active at the end of the procedure _
Else _
ThisWorkbook is active at the end of the procedure
'MsgBox ThisWorkbook.Name
Application.ScreenUpdating = True
End Sub