Some useful application properties

When you're working with Excel, only one workbook at a time can be active. And if the sheet is a worksheet, one cell is the active cell (even if a multicell range is selected).

VBA knows this and lets you refer to these active objects in a simplified manner. This is often useful because you won't always know the exact workbook, worksheet, or range that you want to operate on. VBA handles this by providing properties of the Application object. For example, the Application object has an ActiveCell property that returns a reference to the active cell. The following instruction assigns the value 1 to the active cell:

ActiveCell.Value = 1

Notice that I omitted the reference to the Application object in the preceding example because it is assumed. It's important to understand that this instruction will fail if the active sheet is not a worksheet. For example, if VBA executes this statement when a chart sheet is active, the procedure halts, and you'll get an error message.

If a range is selected in a worksheet, the active cell will be in a cell within the selected range. In other words, the active cell is always a single cell (never a multi-cell range).

The Application object also has a Selection property that returns a reference to whatever is selected, which could be a single cell (the active cell), a range of cells, or an object such as ChartObject, TextBox, or Shape.

Table 7-3 lists the other Application properties that are useful when working with cells and ranges.


Property Object Returned

ActiveCell The active cell.

ActiveChart The active chart sheet or chart contained in a ChartObject on a worksheet. This property will be Nothing if a chart is not active.

ActiveSheet The active sheet (worksheet or chart).

ActiveWindow The active window.

ActiveWorkbook The active workbook.

RangeSelection The selected cells on the worksheet in the specified window, even when a graphic object is selected.

Selection The object selected. (It could be a Range object, Shape,

ChartObject, and so on.)

ThisWorkbook The workbook that contains the procedure being executed.

The advantage of using these properties to return an object is that you don't need to know which cell, worksheet, or workbook is active; nor do you have to provide a specific reference to it. This allows you to write VBA code that is not specific to a particular workbook, sheet, or range. For example, the following instruction clears the contents of the active cell, even though the address of the active cell is not known:


The example that follows displays a message that tells you the name of the active sheet:

MsgBox ActiveSheet.Name

If you want to know the name of the active workbook, use a statement like this:

MsgBox ActiveWorkbook.Name

If a range on a worksheet is selected, you can fill the entire range with a value by executing a single statement. In the following example, the Selection property of the Application object returns a Range object that corresponds to the selected cells. The instruction simply modifies the Value property of this Range object, and the result is a range filled with a single value:

Selection.Value = 12

Note that if something other than a range is selected (such as a ChartObject or a Shape), the preceding statement will generate an error because ChartObject and Shape objects do not have a Value property.

The following statement, however, enters a value of 12 into the Range object that was selected before a non-Range object was selected. If you look up the RangeSelection property in the Help system, you'll find that this property applies only to a Window object.

ActiveWindow.RangeSelection.Value = 12

To find out how many cells are selected in the active window, access the Count property. Here's an example:

MsgBox ActiveWindow.RangeSelection.Count

0 0

Post a comment