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

帅比萌擦擦* 提交于 2019-12-10 19:28:16

问题


I am in the habit of of always using ActiveSheet like this: ActiveWorkbook.ActiveSheet. I recently stumbled across this Microsoft page with code that includes ActiveSheet without ActiveWorkbook. The title of that page is Application.ActiveSheet Property.

Is there a difference between these three lines of code?

ActiveSheet
ActiveWorkbook.ActiveSheet
Application.ActiveSheet

回答1:


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 either the 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.

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.



来源:https://stackoverflow.com/questions/45023938/how-do-activesheet-activeworkbook-activesheet-and-application-activesheet-behav

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!