Microsoft Office Tutorials and References
In Depth Information
Figure 19-2. The range as a union
As another illustration, consider the range selected in Figure 19-2 . This range is the union B4:C5,
E2:E7.
Figure 19-2. The range as a union
The code:
Dim rng As Range
Set rng = Range("B4:C5, E2:E7")
MsgBox rng.Columns(1).Cells(1, 1).Value
displays a message box containing the x shown in cell B4 in Figure 19-2 because the indexes in
the Cells property are taken relative to the upper cell in the leftmost area in the range.
Note that we can use either integers or characters (in quotes) to denote a column, as in:
Columns(5)
and:
Columns("E")
We can also write, for instance:
Columns("A:D")
to denote columns A through D. Similarly, we can denote multiple rows as in:
Rows("1:3")
Since a syntax such as:
Columns("C:D", "G:H")
does not work, the Union method is often useful in connection with the Columns and Rows
methods. For instance, the code:
Dim rng As Range
Set rng = Union(Rows(3), Rows(5), Rows(7))
rng.Select
selects the third, fifth, and seventh rows of the worksheet containing this code or of the active
worksheet if this code is in a workbook or standard code module.
 
 
Search JabSto ::




Custom Search