Properties Can Return Objects

In the discussion at the start of this chapter, I said that the basic syntax of VBA is Object.Method. In this particular line of code, the method is .Select. The End keyword is a property, but from the help file, you see that it returns a Range object. Because the .Select method can apply to a Range object, the method is actually appended to a property.

You might then assume that Selection is the object in this line of code. If you click the mouse in the word Selection and press F1, you will see that according to the help topic, Selection is actually a property and not an object. In reality, the proper code would be to say Application.Selection; however, when you are running within Excel, VBA assumes you are referring to the Excel object model, so you can leave off the Application object. If you were to write a program in Word VBA to automate Excel, you would be required to include an object variable before the .Selection property to qualify to which application you are referring.

In this case, the Application.Selection can return several different types of objects. If a cell is selected, it returns the Range object.

0 0

Post a comment