次の方法で共有


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 を作成します。 次に、PivotTableLocationInTablePivotCellPivotItem、および PivotField の各プロパティを使用して、ピボットテーブル レポート内の NamedRange の配置情報を表示します。 また、Group メソッドを使用して、フィールドの 1 番目の値に基づいて数値によるグループ化を実行します。

この例は、ドキュメント レベルのカスタマイズ用に作成されています。

    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", missing].Value2 = "Date";
    this.Range["A2", missing].Value2 = "March 1";
    this.Range["A3", missing].Value2 = "March 8";
    this.Range["A4", missing].Value2 = "March 15";

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

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

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

    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", missing], "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, missing, missing, missing);
}

.NET Framework セキュリティ

  • 直前の呼び出し元に対する完全な信頼。 このメンバーは、部分的に信頼されているコードから使用することはできません。 詳細については、「部分信頼コードからのライブラリの使用」を参照してください。

参照

参照

NamedRange インターフェイス

Microsoft.Office.Tools.Excel 名前空間