Microsoft Office Tutorials and References
In Depth Information
Some Useful Application Properties
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 about
active workbooks, worksheets, and cells and lets you refer to these active objects in a simplified
manner. This method of referring to objects is often useful because you won’t always know the
exact workbook, worksheet, or range that you want to operate on. VBA makes it easy 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’s assumed. It’s important to understand that this instruction will fail if the active sheet
isn’t a worksheet. For example, if VBA executes this statement when a chart sheet is active, the
procedure halts, and you get an error message.
If a range is selected in a worksheet, the active cell is a cell within the selected range. In other
words, the active cell is always a single cell (never a multicell range).
The Application object also has a Selection property that returns a reference to whatever
is selected, which may 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
Table 7-3: Some Useful Properties of the Application Object
Object Returned
The active cell.
The active chart sheet or chart contained in a ChartObject on a worksheet.
This property is Nothing if a chart isn’t active.
The active sheet (worksheet or chart).
The active window.
The active workbook.
The object selected. (It could be a Range object, Shape , ChartObject , and so
The workbook that contains the VBA procedure being executed. This object may
or may not be the same as the ActiveWorkbook object.
Search JabSto ::

Custom Search