Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 832
ChrisB Avatar asked Oct 29 '25 16:10

ChrisB


1 Answers

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.

like image 185
Kevin Avatar answered Oct 31 '25 12:10

Kevin