2012-10-12 71 views
5

我試圖創建數據透視表,但獲取Invalid Procedure Call or Argument如何在VBA中創建數據透視表

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="rng", Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:="rngB", TableName:="pvtReportA_B", DefaultVersion:=xlPivotTableVersion14 
  • rng(源)是由大約20列和幾千行的範圍。
  • rngB(目標)是在不同的工作表中的單個細胞

誰能勸我要去的地方錯了嗎?

編輯:

是我的錯,我應該使用rngData,而不是rng作爲源已。

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 

這將調出數據透視表框架就好了。

+0

那麼,一切都好?問題已關閉? – nutsch

+0

您可以用一行代碼設置數據範圍: 'Set rngData = range(wsA.Range(「C14」),wsA.Range(「C14」)。end(xlToRight).end(xlDown))' –

回答

4

在這個例子中,我使用了錯誤的範圍對象,這導致Excel拋出一個合適的結果。

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

在第三行代碼中,您應該將'rngData'而不是'rng' – amg

3

Excel 2010中創建一個支點,使用VBA代碼,你可以使用和適應這個模板:

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 

我發現它here

this page您還可以罰款代碼的每個部分的含義,例如解釋here。 我認爲這也是一個很好的代碼,可以開始爲Excel 2007或其他版本創建vba宏。