18.11.2014 Views

Microsoft Office

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Part VI<br />

Programming Excel with VBA<br />

n Examine an object’s current property setting and take some action based on it.<br />

n Change an object’s property setting.<br />

You refer to a property in your VBA code by placing a period (a dot) and the property name after the<br />

object’s name. For example, the following VBA statement sets the Value property of a range named frequency<br />

to 15. (That is, the statement causes the number 15 to appear in the range’s cells.)<br />

Range(“frequency”).Value = 15<br />

Some properties are read-only, which means that you can examine the property, but you can’t change the<br />

property. For a single-cell Range object, the Row and Column properties are read-only properties: You can<br />

determine where a cell is located (in which row and column), but you can’t change the cell’s location by<br />

changing these properties.<br />

A Range object also has a Formula property, which is not read-only; that is, you can insert a formula into<br />

a cell by changing its Formula property. The following statement inserts a formula into cell A1 by changing<br />

the cell’s Formula property:<br />

NOTE<br />

Range(“A11”).Formula = “=SUM(A1:A10)”<br />

Contrary to what you may think, Excel doesn’t have a Cell object. When you want to manipulate<br />

a single cell, you use the Range object (with only one cell in it).<br />

At the top of the object hierarchy is the Application object, which is actually Excel, the program. The<br />

Application object has several useful properties:<br />

n<br />

n<br />

n<br />

Application.ActiveWorkbook: Returns the active workbook (a Workbook object) in<br />

Excel.<br />

Application.ActiveSheet: Returns the active sheet (a Sheet object) of the active<br />

workbook.<br />

Application.ActiveCell: Returns the active cell (a Range object) object of the active<br />

window.<br />

n Application.Selection: Returns the object that is currently selected in the active window<br />

of the Application object. This can be a Range, a Chart, a Shape, or some other selectable<br />

object.<br />

You also should understand that properties can return objects. In fact, that’s exactly what the preceding<br />

examples do. The result of Application.ActiveCell, for example, is a Range object. Therefore, you<br />

can access properties by using a statement such as the following:<br />

Application.ActiveCell.Font.Size = 15<br />

In this case, the ActiveCell property returns a Range object. The Font property returns a Font object,<br />

which is contained in the Range object. Size is a property of the Font object. The preceding statement<br />

sets the Size property to 15 — that is, it causes the font in the currently selected cell to have a size of 15<br />

points.<br />

TIP<br />

Because Application properties are so commonly used, you can omit the object qualifier<br />

(Application). For example, to get the row of the active cell, you can use a statement such<br />

as the following:<br />

ActiveCell.Row<br />

698

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

Saved successfully!

Ooh no, something went wrong!