2012-10-12 103 views
5

Estoy intentando crear una tabla Pivot, pero obteniendo Invalid Procedure Call or Argument.Cómo crear una tabla dinámica en VBA

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="rng", Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:="rngB", TableName:="pvtReportA_B", DefaultVersion:=xlPivotTableVersion14 
  • rng (la fuente) es una gama que consta de alrededor de 20 columnas y unos pocos miles de filas.
  • rngB (El destino) es una sola celda en una hoja de cálculo diferente

¿Alguien puede aconsejar dónde voy mal?

EDIT:

Mi culpa, debería haber estado usando rngData y no rng como fuente.

Set rng = wsA.Range("C14") 
    Set rngData = Range(rng, rng.End(xlToRight)) 
    Set rngData = Range(rng, rng.End(xlDown)) 
    Set rngB = wsB.Range("C8") 

    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngData, Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:=rngB, TableName:="pvtReportA_B", DefaultVersion:=xlPivotTableVersion14 

Esto abre el cuadro de la tabla dinámica muy bien.

+0

Entonces, todo bien? Pregunta cerrada? – nutsch

+0

Puede establecer el rango de datos con una línea de código: 'Establecer rngData = rango (wsA.Range (" C14 "), wsA.Range (" C14 "). End (xlToRight) .end (xlDown))' –

Respuesta

4

En este caso, utilicé el objeto de rango incorrecto, lo que provocó que Excel arrojara un ajuste.

Set rng = wsA.Range("C14") 
Set rngData = Range(rng, rng.End(xlToRight)) 
Set rngData = Range(rng, rng.End(xlDown)) 
Set rngB = wsB.Range("C8") 

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngData, Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:=rngB, TableName:="pvtReportA_B", DefaultVersion:=xlPivotTableVersion14 
+1

En la tercera línea de código debe poner 'rngData' en lugar de 'rng' – amg

3

Para crear un pivote en Excel 2010 , utilizando código VBA, puede utilizar y adaptar esta plantilla:

Sub newPVT() 
    Dim PTCache As PivotCache 
    Dim PT As PivotTable 

    'Create the Cache 
    Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _ 
     SourceData:=Range("Dynamic_Field_Summary")) 

    'Select the destination sheet 
    Sheets("Field Summary").Select 

    'Create the Pivot table 
    Set PT = ActiveSheet.PivotTables.Add(PivotCache:=PTCache, _ 
     TableDestination:=Range("P1"), TableName:="Pivot1") 

    ActiveWorkbook.ShowPivotTableFieldList = True 

    'Adding fields 
    With PT 
     With .PivotFields("Enterprise") 
      .Orientation = xlColumnField 
      .Position = 1 
     End With 

     With .PivotFields("Field") 
      .Orientation = xlRowField 
      .Position = 1 
     End With 

     With .PivotFields("Planted Acres") 
      .Orientation = xlDataField 
      .Position = 1 
      .Caption = " Planted Acres" 
      .Function = xlSum 
     End With 

     With .PivotFields("Harvested Acres") 
      .Orientation = xlDataField 
      .Position = 2 
      .Caption = " Harvested Acres" 
      .Function = xlSum 
     End With 

     With .PivotFields("lbs") 
      .Orientation = xlDataField 
      .Position = 3 
      .Caption = " lbs" 
      .Function = xlSum 
     End With 

     'Adjusting some settings 
     .RowGrand = False 
     .DisplayFieldCaptions = False 
     .HasAutoFormat = False 

     'Improving the layout 
     .TableStyle2 = "PivotStyleMedium9" 
     .ShowTableStyleRowStripes = True 
     .ShowTableStyleColumnStripes = True 

    End With 

    With ActiveSheet 
     'Adjusting columns width 
     .Columns("P:V").ColumnWidth = 16 
     .Range("Q2:V2").HorizontalAlignment = xlCenter 
    End With 

    ActiveWorkbook.ShowPivotTableFieldList = False 
End Sub 

me pareció here.

En this page también se puede refinar el significado de cada parte del código, por ejemplo se explica here. Creo que este es un buen código también para comenzar a crear macros vba para Excel 2007 u otras versiones.

Cuestiones relacionadas