Range Object
| Excel Developer Reference |
Remarks
The following properties and methods for returning a Range object are described in the examples section:
Example
Use Range(
| Visual Basic for Applications |
|---|
|
The following example fills the range A1:H8 with random numbers by setting the formula for each cell in the range. When it’s used without an object qualifier (an object to the left of the period), the Range property returns a range on the active sheet. If the active sheet isn’t a worksheet, the method fails. Use the Activate method to activate a worksheet before you use the Range property without an explicit object qualifier.
| Visual Basic for Applications |
|---|
|
The following example clears the contents of the range named Criteria.
Note |
|---|
| If you use a text argument for the range address, you must specify the address in A1-style notation (you cannot use R1C1-style notation). |
| Visual Basic for Applications |
|---|
|
Use Cells(
| Visual Basic for Applications |
|---|
|
The following example sets the formula for cell A2.
| Visual Basic for Applications |
|---|
|
Although you can also use Range("A1") to return cell A1, there may be times when the Cells property is more convenient because you can use a variable for the row or column. The following example creates column and row headings on Sheet1. Notice that after the worksheet has been activated, the Cells property can be used without an explicit sheet declaration (it returns a cell on the active sheet).
Note |
|---|
Although you could use Visual Basic string functions to alter A1-style references, it's much easier (and much better programming practice) to use the Cells(1, 1) notation. |
| Visual Basic for Applications |
|---|
|
Use
| Visual Basic for Applications |
|---|
|
Use Range(
Note |
|---|
| Notice the period in front of each occurrence of the Cells property. The period is required if the result of the preceding With statement is to be applied to the Cells property — in this case, to indicate that the cells are on worksheet one (without the period, the Cells property would return cells on the active sheet). |
| Visual Basic for Applications |
|---|
|
Use Offset(
| Visual Basic for Applications |
|---|
|
Use Union(
| Visual Basic for Applications |
|---|
|
If you work with selections that contain more than one area, the Areas property is very useful. It divides a multiple-area selection into individual Range objects and then returns the objects as a collection. You can use the Count property on the returned collection to check for a selection that contains more than one area, as shown in the following example.
| Visual Basic for Applications |
|---|
|
Reference
Using Defined Names
Under the Example heading, there are two lines that read
Use Range(
arg), where arg names the range...This should read:
Use Range(arg), where
arg names the range...There's a similar typo several more times on the page.
- 5/1/2008
- Thomas Lee
- 8/1/2008
- Stanley Roark
Note