다음을 통해 공유


NamedRange.Rows Property

Definition

Gets a Range that represents one or more rows in the NamedRange control.

public:
 property Microsoft::Office::Interop::Excel::Range ^ Rows { Microsoft::Office::Interop::Excel::Range ^ get(); };
public Microsoft.Office.Interop.Excel.Range Rows { get; }
member this.Rows : Microsoft.Office.Interop.Excel.Range
Public ReadOnly Property Rows As Range

Property Value

A Range that represents one or more rows in the NamedRange control.

Examples

The following code example creates a NamedRange and then uses the Rows property to set the color, name, size, and boldness of the font of all the cells in the first five rows in the range.

This example is for a document-level customization. To run this code, copy it into one of the worksheet classes in your project.

private void SetRowsFont()
{
    Microsoft.Office.Tools.Excel.NamedRange testRange =
        this.Controls.AddNamedRange(this.Range["A1", "J10"],
        "TestRange");
    testRange.Select();

    Excel.Range fillRows = (Excel.Range)testRange.Rows["1:5"];
    Excel.Font rowsFont = fillRows.Font;

    // Set the font color to blue (RGB value 00 00 FF), and set other font properties.
    rowsFont.Color = 0xFF0000;
    rowsFont.Name = "Arial";
    rowsFont.Size = 14;
    rowsFont.Bold = false;

    // Test the changes by writing a value to all the row cells.
    fillRows.Value2 = "This is a test";
}
Private Sub SetRowsFont()
    Dim testRange As Microsoft.Office.Tools.Excel.NamedRange = _
        Me.Controls.AddNamedRange(Me.Range("A1", "J10"), _
        "TestRange")
    testRange.Select()

    Dim fillRows As Excel.Range = TryCast(testRange.Rows("1:5"), Excel.Range)
    With fillRows.Font
        ' Set the font color to blue (RGB value 00 00 FF), and set other font properties.
        .Color = &HFF0000
        .Name = "Arial"
        .Size = 14
        .Bold = False
    End With

    ' Test the changes by writing a value to all the row cells.
    fillRows.Value2 = "This is a test"
End Sub

The following code example creates a NamedRange and then uses the Rows property to determine how many rows there are in the range.

This example is for a document-level customization. To run this code, copy it into one of the worksheet classes in your project.

Microsoft.Office.Tools.Excel.NamedRange compositeRange;
private void DisplayRangeComposition()
{
    compositeRange = this.Controls.AddNamedRange(
        this.Range["B2", "E5"], "compositeRange");
    compositeRange.Cells.Interior.Color = 0xFF00;
    MessageBox.Show("The range has " + compositeRange.Count + 
        " cells.");
    MessageBox.Show("The range has " + 
        compositeRange.Columns.Count + " columns.");
    MessageBox.Show("The range has " + 
        compositeRange.Rows.Count + " rows.");
}
Private compositeRange As Microsoft.Office.Tools.Excel.NamedRange

Private Sub DisplayRangeComposition()
    compositeRange = Me.Controls.AddNamedRange( _
        Me.Range("B2", "E5"), "compositeRange")
    compositeRange.Cells.Interior.Color = &HFF00
    MessageBox.Show("The range has " & _
        compositeRange.Count & " cells.")
    MessageBox.Show("The range has " & _
        compositeRange.Columns.Count & " columns.")
    MessageBox.Show("The range has " & _
        compositeRange.Rows.Count & " rows.")
End Sub

Remarks

When used without parameters, this property returns a Range object that contains all the rows in the named range.

This property can be used with the following optional parameters to get specific rows in the named range. If you use this property with parameters, the return value is an object that must be cast to a Range.

Parameter Description
RowIndex The index of one or more rows to get.

To get a single row, pass an integer that specifies the index of the row you want to get. The row indexes begin at 1.

To get multiple contiguous rows, pass a string with the format "first row:last row". For example, to get rows 1 through 5, pass "1:5".
ColumnIndex Do not use this parameter. This property will throw a COMException if you try to pass a value to this parameter.

Applies to