Range Method

Applies To

Application Object, Range Object, Worksheet Object.

Description

Accessor. Returns a Range object that represents a cell or range of cells.

Syntax 1

object.Range(cell1)

Syntax 2

object.Range(cell1, cell2)

object

Optional for Application, required for Range and Worksheet. The object to which this method applies.

cell1

Required for Syntax 1. The name of the range. This must be an A1-style reference in the language of the macro. It may include the range operator ':' (colon), the intersection operator ' ' (space), or the union operator ',' (comma). It may include dollar signs, but they are ignored. Any part of the range may use a local defined name. If you use a name, the name is assumed to be in the language of the macro.

cell1, cell2

Required for Syntax 2. The cells at the top left and bottom right of the range. Each one may be a Range containing exactly a single cell (or an entire column or entire row), or a string naming a single cell in the language of the macro.

Remarks

When used with no object qualifier, this method is a shortcut for ActiveSheet.Range (it returns a range from the active sheet; if the active sheet is not a worksheet, the method fails).

When applied to a Range object, the method is relative to the Range object. For example, if the selection is cell C3, then Selection.Range("B1") returns cell D3 because it is relative to the Range object returned by the Selection property. On the other hand, the code ActiveSheet.Range("B1") always returns cell B1.

See Also

Cells Method.

Example

This example sets the value of cell A1 on Sheet1 to 3.14159.


Worksheets("Sheet1").Range("A1").Value = 3.14159

This example creates a formula in cell A1 on Sheet1.


Worksheets("Sheet1").Range("A1").Formula = "=10*RAND()"

This example loops on cells A1:D10 on Sheet1. If one of the cells has a value less than 0.001, the code replaces the value with 0 (zero).


For Each c in Worksheets("Sheet1").Range("A1:D10")
    If c.Value < .001 Then
        c.Value = 0
    End If
Next c

This example loops on the range named "TestRange" and displays the number of empty cells in the range.


numBlanks = 0
For Each c In Range("TestRange")
    If c.Value = "" Then
        numBlanks = numBlanks + 1
    End If
Next c
MsgBox "There are " & numBlanks & " empty cells in this range"

This example sets the font in cells A1:C5 on Sheet1 to italic. The example uses Syntax 2 of the Range method.


Worksheets("Sheet1").Activate
Range(Cells(1, 1), Cells(5, 3)).Font.Italic = True