The Range Property
You will use the Range property to return a Range object in many different circumstances. The Application object, the Worksheet object, and the Range object all have a Range property. The Application object's Range property returns the same Range object as that returned by the Worksheet object. In other words, the Application object's Range property returns a reference to the specified cell or cells on the active worksheet. The Range property of the Range object has a subtle difference that is important to understand. Consider the following example:
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Set rng1 = Application.Range("B5")
Set rng2 = Worksheets("Sheet1").Range("B5")
Set rng3 = rng2.Range("B5")
The three Range objects do not all return a reference to the same cell. In this example, rng1
and rng2
both return a reference to cell B5. But rng3
returns a reference to cell C9. This difference occurs because the Range object's Range property returns a reference relative to the specified cell. In this case, the specified cell is B5. Therefore, the "B" means that the reference will be one column to the right of B5, and the "5" means the reference will be the fifth row below the row specified by B5. In other words, the Range object's Range property returns a reference to a cell that is n columns to the right and y rows down from the specified cell.
Typically, you will use the Range property to return a Range object, and then use the properties and methods of that Range object to work with the data in a cell or group of cells. The following table contains several examples illustrating usage of the Range property.
To | Use this code |
---|---|
Set the value of cell A1 on Sheet1 to 100 |
|
Set the value for a group of cells on the active worksheet |
|
Set the formula for cell B15 on the active worksheet |
|
Set the font to bold |
|
Set the font color to green |
|
Set an object variable to refer to a single cell |
|
Set an object variable to refer to a group of cells |
|
Format all the cells in a named range |
|
Set an object variable to a named range |
|
Set an object variable representing all the used cells on the Employees worksheet |
|
Set an object variable representing the group of related cells that surround the active cell |
|
Set an object variable representing the first three columns in the active worksheet |
|
Set an object variable representing rows 3, 5, 7, and 9 of the active worksheet |
|
Set an object variable representing multiple noncontiguous groups of cells on the active sheet |
|
Remove the contents for all cells within a specified group of cells (B5:B10) while leaving the formatting intact |
|
As you can see from the examples in the preceding table, the Cell argument of the Range property is either an A1-style string reference or a string representing a named range within the current workbook.
You will also use the Range property to return Range objects as arguments to other methods in the Microsoft® Excel object model. When you use the Range property in this way, make sure you fully qualify the Worksheet object to which the Range property applies. Failing to use fully qualified references to the Range property in arguments for Excel methods is one of the most common sources of error in range-related code.
See Also
Working with Microsoft Excel Objects | Understanding the Range Object | The ActiveCell and Selection Properties | Using the CurrentRegion and UsedRange Properties | Using the Cells Property | Using the Offset Property