Skip to main content

Areas object

Table of contents
  1. Remarks
  2. Example

A collection of the areas, or contiguous blocks of cells, within a selection.

Remarks

There's no singular Area object; individual members of the Areas collection are Range objects. The Areas collection contains one Range object for each discrete, contiguous range of cells within the selection. If the selection contains only one area, the Areas collection contains a single Range object that corresponds to that selection.

Example

Use the Areas property of the Range object to return the Areas collection. The following example clears the current selection if it contains more than one area.

If Selection.Areas.Count <> 1 Then Selection.Clear

Use Areas (index), where index is the area index number, to return a single Range object from the collection. The index numbers correspond to the order in which the areas were selected. The following example clears the first area in the current selection if the selection contains more than one area.

If Selection.Areas.count <> 1 Then
    Selection.Areas(1).Clear
End If

Some operations cannot be performed on more than one area in a selection at the same time; you must loop through the individual areas in the selection and perform the operations on each area separately. The following example performs the operation named myOperation on the selected range if the selection contains only one area; if the selection contains multiple areas, the example performs myOperation on each individual area in the selection.

Set rangeToUse = Selection
If rangeToUse.Areas.count = 1 Then
    myOperation rangeToUse
Else
    For Each singleArea In rangeToUse.Areas
        myOperation singleArea
    Next
End If

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>