NamedRange.Group (Método)

Cuando el control NamedRange representa una sola celda en el rango de datos de un campo de tabla dinámica, el método Group realiza una agrupación numérica o de fechas en ese campo.

Espacio de nombres:  Microsoft.Office.Tools.Excel
Ensamblado:  Microsoft.Office.Tools.Excel (en Microsoft.Office.Tools.Excel.dll)

Sintaxis

'Declaración
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
)

Parámetros

  • Start
    Tipo: System.Object
    Primer valor que se va a agrupar.Si este argumento se omite o es true, se utiliza el primer valor del campo.
  • End
    Tipo: System.Object
    Último valor que se va a agrupar.Si este argumento se omite o es true, se utiliza el último valor del campo.
  • By
    Tipo: System.Object
    Si el campo es numérico, este argumento especifica el tamaño de cada grupo.Si el campo es una fecha, este argumento especifica el número de días de cada grupo si el elemento 4 de la matriz Periods es true y todos los demás elementos son false.De lo contrario, se omite.En cuyo caso, Microsoft Office Excel elige automáticamente un tamaño de grupo predeterminado.
  • Periods
    Tipo: System.Object
    Matriz de siete valores booleanos que especifican el período para el grupo, tal y como se muestra:
    1 - segundos
    2 - minutos
    3 - horas
    4 - días
    5 - meses
    6 - trimestres
    7 - años
    Si un elemento de la matriz es true, se crea un grupo para el período correspondiente; si el elemento es false, no se crea ningún grupo.Si el campo no es un campo de fecha, se omite este argumento.

Valor devuelto

Tipo: System.Object

Comentarios

El control NamedRange debe ser una celda única en el rango de datos del campo de la tabla dinámica. Si intenta aplicar este método a varias celdas, se produce un error (sin que se muestre ningún mensaje de error).

Parámetros opcionales

Para obtener información sobre parámetros opcionales, vea Parámetros opcionales en las soluciones de Office.

Ejemplos

En el ejemplo de código siguiente se crea un informe de tabla dinámica y un control NamedRange dentro del área del informe de tabla dinámica. A continuación, se utilizan las propiedades: PivotTable, LocationInTable, PivotCell, PivotItem y PivotField para mostrar información sobre la ubicación de NamedRange dentro del informe de tabla dinámica. En el ejemplo también se utiliza el método Group para realizar la agrupación numérica por el primer valor del campo.

Se trata de un ejemplo para una personalización en el nivel del documento.

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

Seguridad de .NET Framework

Vea también

Referencia

NamedRange Interfaz

Microsoft.Office.Tools.Excel (Espacio de nombres)