18.11.2014 Views

Microsoft Office

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Introducing Visual Basic for Applications 39<br />

n<br />

n<br />

You specify methods by combining the object with the method, separated by a period. For<br />

example, to clear the contents of cell A1, use the following statement:<br />

Worksheets(“Sheet1”).Range(“A1:C12”).ClearContents<br />

VBA also includes all the constructs of modern programming languages, including variables,<br />

arrays, looping, and so on.<br />

The preceding describes VBA in a nutshell. Now you just have to learn the details, some of which are covered<br />

in the rest of this chapter.<br />

Objects and collections<br />

VBA is an object-oriented language, which means that it manipulates objects, such as Ranges, Charts,<br />

AutoShapes, and so on. These objects are arranged in a hierarchy. The Application object (which is<br />

Excel) contains other objects. For example, the Application object contains a number of objects, including<br />

the following:<br />

n<br />

n<br />

AddIns (a collection of AddIn objects)<br />

Windows (a collection of Window objects)<br />

n Workbooks (a collection of Workbook objects)<br />

Most of these objects can contain other objects. For example, a Workbook object can contain the following<br />

objects:<br />

n<br />

n<br />

n<br />

n<br />

Charts (a collection of Chart sheet objects)<br />

Names (a collection of Name objects)<br />

Styles (a collection of Style objects)<br />

Windows (a collection of Window objects in the workbook)<br />

n Worksheets (a collection of Worksheet objects)<br />

Each of these objects, in turn, can contain other objects. A Worksheet object, for example, can contain the<br />

following objects:<br />

n<br />

n<br />

ChartObjects (a collection of all ChartObject objects)<br />

PageSetup (an object that stores printing information)<br />

n PivotTables (a collection of all PivotTable objects)<br />

A collection consists of all like objects. For example, the collection of all Workbook objects is known as the<br />

Workbooks collection. You can refer to an individual object in a collection by using an index number or a<br />

name. For example, if a workbook has three worksheets (named Sheet1, Sheet2, and Sheet3), you can refer<br />

to the first object in the Worksheets collection in either of these ways:<br />

Worksheets(1)<br />

Worksheets(“Sheet1”)<br />

Properties<br />

The objects that you work with have properties, which you can think of as attributes of the objects. For<br />

example, a Range object has properties, such as Column, Row, Width, and Value. A Chart object has<br />

properties, such as Legend, ChartTitle, and so on. ChartTitle is also an object, with properties such<br />

as Font, Orientation, and Text. Excel has many objects, and each has its own set of properties. You<br />

can write VBA code to do the following:<br />

697

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!