The Macro Recorder generated the following statement:
Cells.Select
Now I understand that without the object qualifier this will return all the cells as a Range object.
However, I am wondering what the fully qualified version of this statement is?
Is it:
Application.Cells.SelectApplication.ActiveSheet.CellsApplication.ActiveWorkbook.ActiveSheet.Cells
In other words, which one of those fully qualified statements is actually executed by VBE when it runs Cells.Select?
What is the difference between all of these??? As all of these access the same object in the end - is it just personal preference as to which statement I would use if I wanted to explicitly qualify all the objects?
Thank you so much!


Let's take the post apart:
That's actually somewhat incorrect. While it is true that
.Cellsreturns aRange.Cellsobject which returns all the cells,Cells.Selectis actually a method of theRangeobject which - as you may have guessed -Selects the range (in our case, all the cells)The
Selectmethod, as per MSDN actually returns aVariantand not aRangeobject. That it is a pretty important distinction to make, especially if you plan on passing that value to anything. So if we pretended to be a compilerCells->ActiveWorkbook.ActiveSheet.Range.CellsreturnsRangeof all the cellsRange.Cells.Select-> first we take our returnedRange, we then select the cells in Worksheet and actually return aVariant(notRange)As to the other part of the question. It depends where your module is placed. By default,
Cellsis shorthand for the following statement:This however is subject to change depending on where your module is placed and if Application, workbook or sheet has been modified.
In general, it is a good coding practice to always specify at least a specific
Worksheetobject whenever you're referencing aRange, eg.This is explicit and therefore less error prone and clearer to comprehend, be it for you or anyone working with your code.. You always know what exactly you're working with and not leave it to guesswork.
Obviously, the moment you start working with multiple workbooks, it's a good idea to incorporate
Workbookobjects statements before theSheet. You get my point.Last but not least, whatever you're trying to do, it's probably for the best you avoid using
Select. It's generally not worth it and prone to unexpected behaviour.Check this question here: How to avoid using Select in Excel VBA