vba ActiveSheet、ActiveWorkbook.ActiveSheet 和 Application.ActiveSheet 的行为有何不同?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45023938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do ActiveSheet, ActiveWorkbook.ActiveSheet and Application.ActiveSheet behave differently?
提问by ChrisB
I am in the habit of of always using ActiveSheet
like this: ActiveWorkbook.ActiveSheet
. I recently stumbled across this Microsoft pagewith code that includes ActiveSheet
without ActiveWorkbook
. The title of that page is Application.ActiveSheet
Property.
我习惯于总是这样使用ActiveSheet
:ActiveWorkbook.ActiveSheet
. 我最近偶然发现了这个 Microsoft 页面,其中包含ActiveSheet
不包含ActiveWorkbook
. 该页面的标题是Application.ActiveSheet
属性。
Is there a difference between these three lines of code?
这三行代码有区别吗?
ActiveSheet
ActiveWorkbook.ActiveSheet
Application.ActiveSheet
回答by Kevin
These all refer to the same thing the active sheet, but the context may not be as expected. ActiveSheet
is the least restrictive and refers to whatever the 'current' Workbook may be.
ActiveWorkbook.ActiveSheet
just makes it a little more clear. Contrast this with Thisworkbook
which is the workbook where the code is running.
Application.ActiveSheet
refers to eitherthe active workbook or the named workbook / window.
If you have more than one workbook open, it is always best to be explicit about the object you are referring to.
这些都指的是相同的东西active sheet,但上下文可能与预期不同。ActiveSheet
是限制最少的,指的是“当前”工作簿可能是什么。
ActiveWorkbook.ActiveSheet
只是让它更清楚一点。将此与Thisworkbook
代码运行所在的工作簿进行对比。
Application.ActiveSheet
指的是活动工作簿或命名的工作簿/窗口。如果您打开了多个工作簿,最好明确说明您所指的对象。
Important note: When alone and written in the code module ThisWorkbook
of some workbook, ActiveSheet
differs from both Application.ActiveSheet
and from ActiveWorkbook.ActiveSheet
(which are always the same). In this special case, ActiveSheet
refers to ThisWorkbook.ActiveSheet
even if ThisWorkbook
is not the active workbook of the Excel application.
重要提示:当单独写在ThisWorkbook
某个工作簿的代码模块中时,ActiveSheet
与两者Application.ActiveSheet
和 from不同ActiveWorkbook.ActiveSheet
(它们总是相同的)。在这种特殊情况下,ActiveSheet
指的是ThisWorkbook.ActiveSheet
即使ThisWorkbook
不是 Excel 应用程序的活动工作簿。