Compartilhar via


NamedRange.Group(Object, Object, Object, Object) Método

Definição

Quando o controle NamedRange representa uma única célula no intervalo de dados de um campo da Tabela Dinâmica, o método Group(Object, Object, Object, Object) executa o agrupamento numérico ou baseado em data nesse campo.

public object Group (object Start, object End, object By, object Periods);
abstract member Group : obj * obj * obj * obj -> obj
Public Function Group (Optional Start As Object, Optional End As Object, Optional By As Object, Optional Periods As Object) As Object

Parâmetros

Start
Object

O primeiro valor a ser agrupado. Se esse argumento for omitido ou for true, o primeiro valor no campo será usado.

End
Object

O último valor a ser agrupado. Se esse argumento for omitido ou for true, o último valor no campo será usado.

By
Object

Se o campo for numérico, esse argumento especificará o tamanho de cada grupo. Se o campo for uma data, esse argumento especificará o número de dias em cada grupo se o Periods elemento 4 na matriz for true e todos os outros elementos forem false. Caso contrário, esse argumento será ignorado. Se esse argumento for omitido, o Microsoft Office Excel escolherá automaticamente um tamanho de grupo padrão.

Periods
Object

Uma matriz de sete valores boolianos que especificam o período para o grupo, conforme mostrado:1 - Seconds2 - Minutes3 - Hours4 - Days5 - Months6 - Quarters7 - YearsIf um elemento na matriz é , um grupo é truecriado para o tempo correspondente; se o elemento for false, nenhum grupo será criado. Se o campo não for um campo de data, esse argumento será ignorado.

Retornos

Exemplos

O exemplo de código a seguir cria um relatório de Tabela Dinâmica e um NamedRange dentro da área de relatório de Tabela Dinâmica. Em seguida, ele usa as propriedades PivotTable, LocationInTable, PivotCell, PivotItem e PivotField para exibir informações sobre o posicionamento do NamedRange dentro do relatório de Tabela Dinâmica. O exemplo também usa o método Group para executar o agrupamento numérico com base no primeiro valor do campo.

Este exemplo destina-se a uma personalização no nível de documento.

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);
}
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

Comentários

O NamedRange controle deve ser uma única célula no intervalo de dados do campo de Tabela Dinâmica. Se você tentar aplicar esse método a mais de uma célula, ele falhará (sem exibir uma mensagem de erro).

Parâmetros Opcionais

Para obter informações sobre parâmetros opcionais, consulte Parâmetros opcionais em soluções do Office.

Aplica-se a