Writing Excel Macros with VBA by Steven Roman PhD

Writing Excel Macros with VBA by Steven Roman PhD

Author:Steven Roman, PhD [Steven Roman, PhD]
Language: eng
Format: epub
Tags: COMPUTERS / Programming Languages / Visual BASIC
ISBN: 9780596555238
Publisher: O'Reilly Media
Published: 2008-12-16T16:00:00+00:00


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.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.