Defining Ranges

When you use the Union method, you combine multiple ranges to create one multi-range, or Area Collection. You can reference the entire range selection using the Selection property. This property returns the selected object. You can combine the Selection property with the Areas property to return an Areas collection representing all of the areas selected after performing a union.

The Areas Collection actually contains a collection of the specific areas, or blocks of cells, within a specific selection. Each individual member of the Area Collection is actually a Range object representing a contiguous block of cells with one Range object for each block of cells.

You cannot apply some VBA operations to ranges that contain multiple areas. Therefore, you may need to determine the number of areas within a specific range. In order to do this, you can use the Count property. The Count property counts the number of areas within the range, if a value greater than 1 is returned, you know there is more than one area selected. The following example uses the Count property to determine the number of areas within the selected range.

Example:

AreasInNewRange = Selection.Areas.Count.

° Type Set NewRange = Union(Range1, Range2).

Type additional VBA code to work with the new combined range of cells.

■ Excel highlights the combined range of cells.

° Type Set NewRange = Union(Range1, Range2).

Type additional VBA code to work with the new combined range of cells.

■ Excel highlights the combined range of cells.

0 0

Post a comment