2012-10-12 25 views
5

Próbuję utworzyć tabelę przestawną, ale otrzymuję Invalid Procedure Call or Argument.Jak utworzyć tabelę przestawną w języku VBA

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="rng", Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:="rngB", TableName:="pvtReportA_B", DefaultVersion:=xlPivotTableVersion14 
  • rng (źródło) jest zakres składający się z około 20 kolumny i kilku tysięcy rzędach.
  • rngB (docelowy) jest pojedyncza komórka w innym arkuszu

Czy ktoś może doradzić gdzie wezwę niewłaściwy?

EDIT:

moja wina, powinienem być za pomocą rngData a nie rng jako źródło.

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 

Spowoduje to wyświetlenie ramki tabeli przestawnej.

+0

więc wszystko dobrze? Pytanie zamknięte? – nutsch

+0

Można ustawić zakres danych za pomocą jednego wiersza kodu: 'Ustaw wartość rngData = (wsA.Range (" C14 "), wsA.Range (" C14 "). End (xlToRight) .end (xlDown))' –

Odpowiedz

4

W tym przypadku użyłem niewłaściwego obiektu zakresu, który spowodował, że Excel rzucił dopasowanie.

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

W trzecim wierszu kodu powinieneś umieścić "rngData" zamiast "rng" – amg

3

Aby utworzyć pivot w Excel 2010, za pomocą kodu VBA można używać i dostosować ten szablon:

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 

znalazłem here.

W this page możesz również wykończyć znaczenie każdej części kodu, na przykład wyjaśnić here. Myślę, że jest to dobry kod również do rozpoczęcia tworzenia makr vba dla programu Excel 2007 lub innych wersji.

Powiązane problemy