NamedRange.Rows 屬性

定義

取得 Range,表示 NamedRange 控制項中一個或多個列。Gets a Range that represents one or more rows in the NamedRange control.

public Microsoft.Office.Interop.Excel.Range Rows { get; }

屬性值

Range

取得 Range,表示 NamedRange 控制項中一個或多個列。A Range that represents one or more rows in the NamedRange control.

範例

下列程式碼範例會建立 NamedRange ,然後使用 Rows 屬性來設定範圍內前五個數據列中,所有儲存格字型的色彩、名稱、大小和加粗。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

下列程式碼範例會建立 NamedRange ,然後使用 Rows 屬性來判斷範圍中有多少資料列。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

備註

當使用時,如果沒有參數,這個屬性會傳回 Range 物件,其中包含已命名範圍內的所有資料列。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. 如果您將此屬性與參數搭配使用,則傳回值會是必須轉換成的物件 RangeIf 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. 資料列索引從1開始。The row indexes begin at 1.

若要取得多個連續的資料列,請傳遞格式為 " first row :" 的字串 last rowTo get multiple contiguous rows, pass a string with the format "first row:last row". 例如,若要取得1到5個數據列,請傳遞 "1:5"。For example, to get rows 1 through 5, pass "1:5".
ColumnIndex 請勿使用此參數。Do not use this parameter. COMException如果您嘗試將值傳遞給這個參數,這個屬性將會擲回。This property will throw a COMException if you try to pass a value to this parameter.

適用於