2016-07-14 10 views
1

既に作成済みのピボットテーブルにフィールドを自動的に追加しようとしています。フィールドは、「期間1の合計」...「期間360の合計」となります。私はすでに1-60の期間を持っていますが、61-360が欠けています。手動で300回ドラッグするのは苦痛です。だから私は、マクロを書いたが、私はエラーを取得: ピボットテーブルが既に作成されているので、ピボットフィールドクラスピボットテーブル "値"を展開するVBA

のピボットアイテムのプロパティを取得できませんでしが(それは多くの形式の癖を持つ大きなテーブルだ)、Iドン何も変えたくない。私はperiod61〜period360の値の合計としてピリオドを追加したいだけです。

私は自分のコードを添付しました。私が間違っていることが分からず、あまりにも不満を抱かないようにしているので、誰でも私に与えることができる助けは非常に感謝しています。

は事前にありがとうございました。ここで

は私のコードは次のとおりです。

'--------------------- Expand_Pivot_to_360M Macro-------------------' 

Sub Expand_Pivot_to_360M() 

' Setting Pivot field classes to the "unable to get pivot items property of the pivot field class" error 
    Sheets("Monthly Summary").Select 

    With ActiveSheet.PivotTables("PivotTable1").PivotFields("universe") 
     .Orientation = xlRowField 
     .Position = 1 
    End With 
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("buckets_break") 
     .Orientation = xlRowField 
     .Position = 2 
    End With 
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("pool_name") 
     .Orientation = xlRowField 
     .Position = 3 
    End With 
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("gl_company_code") 
     .Orientation = xlRowField 
     .Position = 4 
    End With 
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("LEP") 
     .Orientation = xlRowField 
     .Position = 5 
    End With 

ループPivitを展開するために360の期間

' Using a Loop to Expand Pivot for 360 periods 
    Dim i As Integer 

     For i = 61 To 360  ' start at period 61 since periods 1 to 60 already include in pivot 

      Sheets("Monthly Summary").Select 
      ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables(_ 
       "PivotTable1").PivotFields("Period_i"), "Sum of Period_i", xlSum 
      With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of Period_i") 
       .Caption = "Count of Period_i" 
       .Function = xlCount 
      End With 
      With ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of Period_i") 
       .Caption = "Sum of Period_i" 
       .Function = xlSum 
      End With 

     Next i 

End Sub 
+0

は、「'_'と」 '_iを交換& – Slai

+0

をi'ありがとう、私はそれをやったし、もはや「ピボットアイテムを取得できませんプロパティを取得します。.. "エラーが発生しましたが、別のエラーが発生します:アプリケーション定義またはオブジェクト定義のエラー"。あなたが助けることができるかどうか教えてください。 –

答えて

0

あなたはマクロ記録から生成された余分なコードの多くを持っています。また、追加しようとしているフィールドの1つ以上が既に存在するため、「アプリケーション定義またはオブジェクト定義のエラー」メッセージが表示される可能性があります。私はこのことをお勧めし

...

Dim i as Integer 
Dim found as Boolean 
Dim aPItem as PivotItem 
With Sheets("Monthly Summary").PivotTables("PivotTable1") 
    For i = 61 to 360 
     found = false 
     For Each aPItem In .DataPivotField.PivotItems 'Check field not already there 
      If aPItem.Caption = "Sum of Period_" & i Then 
       found = True 
       Exit For 
      End If 
     Next aPItem 

     If Not found then 'add field 
      .AddDataField .PivotFields("Period_" & i), "Sum of Period_" & i, xlSum 
     End If 
    Next i 
End With 
関連する問題