Читать книгу Excel VBA Programming For Dummies - Dick Kusleika - Страница 69
Working with the Excel Object Model
ОглавлениеThe Excel Object Model is computer code that Microsoft wrote that defines how VBA interacts with Excel. In it, Microsoft has defined hundreds of objects whose names you would recognize as elements of the Excel user interface, like Workbook, Worksheet, or PivotTable. An object is nothing more than computer code that defines properties, methods, and events. It is the unique combination of properties, methods, and events that defines an object. Properties and methods are discussed later in this chapter and events are discussed in Chapter 11.
A car is an object. It has properties like color and number of pistons. If you change the color or the number of pistons, you have a different car, but it’s still a car. Cars have methods like accelerate and brake. Excel objects are a little more abstract. For example, a Worksheet is an object. It has properties like Name and Visible that you can change to rename or hide the worksheet. It has methods like Activate that you can invoke to make it the active worksheet. You can change the properties of a Worksheet object to make it the Worksheet you want, but it’s still a Worksheet because it has that unique set of properties and methods.
Excel’s object model is arranged as a hierarchy. That is, every object is organizationally a parent or a child of another object, like a family tree. There’s no magic to the hierarchy; it’s just how Microsoft designed it. But you won’t be surprised to learn that it mostly mirrors what you see in the user interface. Worksheet objects are children of a Workbook object just like how workbook hold worksheets in Excel. A Worksheet object is the parent of a PivotTable object because PivotTables exist on sheets in Excel.