Share via


NamedRange.Group 方法

NamedRange 控制項表示樞紐分析表欄位之資料範圍中的單一儲存格時,Group 方法會在該欄位中執行以數值或日期為基礎的群組作業。

命名空間:  Microsoft.Office.Tools.Excel
組件:  Microsoft.Office.Tools.Excel (在 Microsoft.Office.Tools.Excel.dll 中)

語法

'宣告
Function Group ( _
    Start As Object, _
    End As Object, _
    By As Object, _
    Periods As Object _
) As Object
Object Group(
    Object Start,
    Object End,
    Object By,
    Object Periods
)

參數

  • Start
    型別:System.Object
    要加入群組的第一個值。如果省略這個引數或者為 true,則使用欄位中的第一個值。
  • End
    型別:System.Object
    要加入群組的最後一個值。如果省略這個引數或者為 true,則使用欄位中的最後一個值。
  • By
    型別:System.Object
    如果欄位為數值,則這個引數會指定每個群組的大小。如果欄位是日期,若 Periods 陣列中的元素 4 為 true,且所有其他元素為 false,則這個引數會指定每個群組的日數。否則,忽略這個引數。如果省略這個引數,Microsoft Office Excel 會自動選擇預設的群組大小。
  • Periods
    型別:System.Object
    用來指定群組的期間,含有 7 個布林值的陣列,如下所示:
    1 - 秒
    2 - 分
    3 - 時
    4 - 日
    5 - 月
    6 - 季
    7 - 年
    如果陣列中的元素為 true,則會以對應的時間建立群組,如果元素為 false,則不會建立群組。如果欄位不是日期欄位,則忽略這個引數。

傳回值

型別:System.Object

備註

NamedRange 控制項必須是樞紐分析表欄位之資料範圍中的單一儲存格。 如果您嘗試將這個方法套用至一個以上的儲存格,則此方法會失敗 (但不顯示任何錯誤訊息)。

選擇性參數

如需選擇性參數的詳細資訊,請參閱Office 方案中的選擇性參數

範例

下列程式碼範例會建立 [樞紐分析表] 報告,同時 [樞紐分析表] 報告的區域中包含 NamedRange。 然後,它會使用 PivotTableLocationInTablePivotCellPivotItemPivotField 屬性,顯示 NamedRange 在 [樞紐分析表] 報告中的位置相關資訊。 範例也使用 Group 方法,根據欄位的第一個值來執行數字分組。

這是示範文件層級自訂的範例。

    Private Sub DisplayPivotTableInformation()
        ' Specify values for the PivotTable.
        Me.Range("A1").Value2 = "Date"
        Me.Range("A2").Value2 = "March 1"
        Me.Range("A3").Value2 = "March 8"
        Me.Range("A4").Value2 = "March 15"

        Me.Range("B1").Value2 = "Customer"
        Me.Range("B2").Value2 = "Smith"
        Me.Range("B3").Value2 = "Jones"
        Me.Range("B4").Value2 = "James"

        Me.Range("C1").Value2 = "Sales"
        Me.Range("C2").Value2 = "23"
        Me.Range("C3").Value2 = "17"
        Me.Range("C4").Value2 = "39"

        ' Create and populate the PivotTable.
        Dim table1 As Excel.PivotTable = _
            Me.PivotTableWizard( _
            Excel.XlPivotTableSourceType.xlDatabase, _
            Me.Range("A1", "C4"), Me.Range("A10"), "Sales Table", _
            False, False, True, False, , , False, False, _
            Excel.XlOrder.xlDownThenOver, , , )

        Dim customerField As Excel.PivotField = _
            CType(table1.PivotFields("Customer"), Excel.PivotField)
        customerField.Orientation = _
            Excel.XlPivotFieldOrientation.xlRowField
        customerField.Position = 1

        Dim dateField As Excel.PivotField = _
            CType(table1.PivotFields("Date"), Excel.PivotField)
        dateField.Orientation = _
            Excel.XlPivotFieldOrientation.xlColumnField
        dateField.Position = 1

        table1.AddDataField(table1.PivotFields("Sales"), _
            "Sales Summary", Excel.XlConsolidationFunction.xlSum)

        ' Create a NamedRange in the PivotTable and display the 
        ' location.
        Dim namedRange1 As Microsoft.Office.Tools.Excel.NamedRange _
            = Me.Controls.AddNamedRange(Me.Range("B11"), _
            "namedRange1")
        namedRange1.Select()

        MessageBox.Show("The NamedRange is in the PivotTable report '" & _
            namedRange1.PivotTable.Name & "' at the location '" & _
            namedRange1.LocationInTable.ToString() & "'.")

        MessageBox.Show("The NamedRange has a PivotCell type of: " & _
            namedRange1.PivotCell.PivotCellType.ToString())

        MessageBox.Show("The NamedRange is in the PivotTable field: " & _
            namedRange1.PivotField.Name)

        MessageBox.Show("The NamedRange is in the PivotTable item: " & _
            namedRange1.PivotItem.Name)

        namedRange1.Group(True, , , )
    End Sub

private void DisplayPivotTableInformation()
{
    // Specify values for the PivotTable.
    this.Range["A1"].Value2 = "Date";
    this.Range["A2"].Value2 = "March 1";
    this.Range["A3"].Value2 = "March 8";
    this.Range["A4"].Value2 = "March 15";

    this.Range["B1"].Value2 = "Customer";
    this.Range["B2"].Value2 = "Smith";
    this.Range["B3"].Value2 = "Jones";
    this.Range["B4"].Value2 = "James";

    this.Range["C1"].Value2 = "Sales";
    this.Range["C2"].Value2 = "23";
    this.Range["C3"].Value2 = "17";
    this.Range["C4"].Value2 = "39";

    // Create and populate the PivotTable.
    Excel.PivotTable table1 = this.PivotTableWizard(
        Excel.XlPivotTableSourceType.xlDatabase,
        this.Range["A1", "C4"],
        this.Range["A10"], "Sales Table", false,
        false, true, false, false, false,
        Excel.XlOrder.xlDownThenOver);

    Excel.PivotField customerField =
        (Excel.PivotField)table1.PivotFields("Customer");
    customerField.Orientation =
        Excel.XlPivotFieldOrientation.xlRowField;
    customerField.Position = 1;

    Excel.PivotField dateField =
        (Excel.PivotField)table1.PivotFields("Date");
    dateField.Orientation =
        Excel.XlPivotFieldOrientation.xlColumnField;
    dateField.Position = 1;

    table1.AddDataField(table1.PivotFields("Sales"),
        "Sales Summary", Excel.XlConsolidationFunction.xlSum);

    // Create a NamedRange in the PivotTable and display the 
    // location.
    Microsoft.Office.Tools.Excel.NamedRange namedRange1 =
        this.Controls.AddNamedRange(
        this.Range["B11"], "namedRange1");
    namedRange1.Select();

    MessageBox.Show("The NamedRange is in the PivotTable report '" +
        namedRange1.PivotTable.Name + "' at the location '" +
        namedRange1.LocationInTable.ToString() + "'.");

    MessageBox.Show("The NamedRange has a PivotCell type of: " +
         namedRange1.PivotCell.PivotCellType.ToString());

    MessageBox.Show("The NamedRange is in the PivotTable field: " +
         namedRange1.PivotField.Name);

    MessageBox.Show("The NamedRange is in the PivotTable item: " +
        namedRange1.PivotItem.Name);

    namedRange1.Group(true);
}

.NET Framework 安全性

請參閱

參考

NamedRange 介面

Microsoft.Office.Tools.Excel 命名空間