Excel 2010 Bible

(National Geographic (Little) Kids) #1

Part VI: Programming Excel with VBA


816


properties: You can determine where a cell is located (in which row and column), but you can’t
change the cell’s location by changing these properties.

A Range object also has a Formula property, which is not read-only; that is, you can insert a for-
mula into a cell by changing its Formula property. The following statement inserts a formula into
cell A12 by changing the cell’s Formula property:

Range(“A12”).Formula = “=SUM(A1:A10)”

Note
Contrary to what you may think, Excel doesn’t have a Cell object. When you want to manipulate a single cell,
you use the Range object (with only one cell in it). n


At the top of the object hierarchy is the Application object, which is actually Excel, the program.
The Application object has several useful properties:

l (^) Application.ActiveWorkbook: Returns the active workbook (a Workbook object)
in Excel.
l (^) Application.ActiveSheet: Returns the active sheet (a Sheet object) of the active
workbook.
l (^) Application.ActiveCell: Returns the active cell (a Range object) object of the
active window.
l (^) Application.Selection: Returns the object that is currently selected in the active
window of the Application object. This can be a Range, a Chart, a Shape, or some
other selectable object.
You also should understand that properties can return objects. In fact, that’s exactly what the pre-
ceding examples do. The result of Application.ActiveCell, for example, is a Range object.
Therefore, you can access properties by using a statement such as the following:
Application.ActiveCell.Font.Size = 15
In this case, the ActiveCell property returns a Range object. The Font property returns a
Font object, which is contained in the Range object. Size is a property of the Font object. The
preceding statement sets the Size property to 15 — that is, it causes the font in the currently
selected cell to have a size of 15 points (pt).
Tip
Because Application properties are so commonly used, you can omit the object qualifier (Application).
For example, to get the row of the active cell, you can use a statement such as the following:
ActiveCell.Row
In many cases, you can refer to the same object in a number of different ways. Assume that you
have a workbook named Sales.xlsx and it’s the only workbook open. Furthermore, assume

Free download pdf