Range Property

You can use the Range property of the Application object to refer to a Range object on the active worksheet. The following example refers to a Range object that is the B2 cell on the currently active worksheet:


Note that you can't test code examples like this one as they are presented. However, as long as you are referring to a range on the active worksheet, these examples can be tested by the Immediate window of the VBE, as follows:


It is important to note that the preceding reference to a Range object will cause an error if there is no worksheet currently active. For example, it will cause an error if you have a chart sheet active.

Because the Range property of the Application object is a member of <globals>, you can omit the reference to the Application object, as follows:


You can refer to more complex Range objects than a single cell. The following example refers to a single block of cells on the active worksheet:


And this code refers to a non-contiguous range of cells:


The Range property also accepts two arguments that refer to diagonally opposite corners of a range. This gives you an alternative way to refer to the A1:D10 range:


Range also accepts names that have been applied to ranges. If you have defined a range of cells with the name SalesData, you can use the name as an argument:


The arguments can be objects as well as strings, which provides much more flexibility. For example, you might want to refer to every cell in column A, from cell A1 down to a cell that has been assigned the name LastCell:


+1 0

Post a comment