Читать книгу Excel VBA Programming For Dummies - Dick Kusleika - Страница 71

Referring to objects

Оглавление

Referring to an object is important because you must identify the object with which you want to work. After all, VBA can’t read your mind — yet. The Application object, being the top dog, is easy. Just type Application and a period (called a dot operator) to get to its properties and methods. For example, if you wanted to change Excel’s behavior so that the cursor doesn’t move when you press Enter, you could write the following code:

Application.MoveAfterReturn = False

Some of the Application object’s properties return other objects. Just like the MoveAfterReturn property return a value (either True or False), a property can also return an object. AutoCorrect is an object in the object model that’s just below the Application object. The way you refer to the AutoCorrect object is by using the AutoCorrect property. Yes, the property and the object it returns have the same name. If you want to tell Excel to fix your typing when you leave Caps Lock on, you would do so with the following code:

Application.AutoCorrect.CorrectCapsLock = True

In this case, CorrectCapsLock is a property of the AutoCorrect object and you are able to address that object via the AutoCorrect property of the Application object.

In the previous section, I wrote that cars are similar to Excel objects because they have properties like color and number of pistons. Cars also have steering wheels and steering wheels are objects, too. They’re just one rung down the car object hierarchy. If a car were a computer program, it would have a property called SteeringWheel that you would use to access the SteeringWheel object.

Workbook names also have a dot to separate the filename from the extension (for example, Book1.xlsx). That’s just a coincidence. The dot in a filename has nothing at all to do with the dot operator referred to a few paragraphs ago.

Excel VBA Programming For Dummies

Подняться наверх