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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:52:39  来源:igfitidea点击:

How do ActiveSheet, ActiveWorkbook.ActiveSheet and Application.ActiveSheet behave differently?

excel-vbaworksheetvbaexcel

提问by ChrisB

I am in the habit of of always using ActiveSheetlike this: ActiveWorkbook.ActiveSheet. I recently stumbled across this Microsoft pagewith code that includes ActiveSheetwithout ActiveWorkbook. The title of that page is Application.ActiveSheetProperty.

我习惯于总是这样使用ActiveSheetActiveWorkbook.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. ActiveSheetis the least restrictive and refers to whatever the 'current' Workbook may be. ActiveWorkbook.ActiveSheetjust makes it a little more clear. Contrast this with Thisworkbookwhich is the workbook where the code is running. Application.ActiveSheetrefers 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 ThisWorkbookof some workbook, ActiveSheetdiffers from both Application.ActiveSheetand from ActiveWorkbook.ActiveSheet(which are always the same). In this special case, ActiveSheetrefers to ThisWorkbook.ActiveSheeteven if ThisWorkbookis not the active workbook of the Excel application.

重要提示:当单独写在ThisWorkbook某个工作簿的代码模块中时,ActiveSheet与两者Application.ActiveSheet和 from不同ActiveWorkbook.ActiveSheet(它们总是相同的)。在这种特殊情况下,ActiveSheet指的是ThisWorkbook.ActiveSheet即使ThisWorkbook不是 Excel 应用程序的活动工作簿。