2017-01-25 35 views
-1

以下はコードの更新です。 TableXは、私のピボットテーブルが使用する参照テーブルです。 'shtReport'変数をワークシートとして宣言し、その変数にさらに名前を付けると、 'Object variableまたはWithブロック変数が設定されていません'というエラーが発生します。以下のコードを使用すると、VBAでPivotTablesの作成と更新の自動化を始めるだろうピボットテーブル記録されたマクロのVBAエラー

Option Explicit 

    Sub SutoPivot() 

    Dim PvtTbl       As PivotTable 
    Dim PvtCache      As PivotCache 
    Dim shtReport      As Worksheet 

    ' set the Pivot Cache (NOT SURE what is "TableX" ?) 
    Set PvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="TableX", Version:=xlPivotTableVersion14) 
    Set shtReport = Worksheets("Invoice Data") 

    ' add this line in case the Pivot table doesn't exit >> first time running this Macro 
    On Error Resume Next 
    Set PvtTbl = Worksheets("Invoice Data").PivotTables("PivotTable1a") 

    On Error GoTo 0 
    If PvtTbl Is Nothing Then 
     ' create a new Pivot Table in "Invoice Data" sheet, start from Cell Y1 
     Set PvtTbl = shtReport.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=Worksheets("Invoice Data").Range("Y1"), _ TableName:="PivotTable1a") 

     With PvtTbl.PivotFields("Sales Director") 
      .Orientation = xlRowField 
      .Position = 1 
     End With 

     With PvtTbl.PivotFields("Manager") 
      .Orientation = xlRowField 
      .Position = 2 
     End With 

     With PvtTbl.PivotFields("Owner") 
      .Orientation = xlRowField 
      .Position = 3 
     End With 

     With PvtTbl.PivotFields("Account Name") 
      .Orientation = xlRowField 
      .Position = 4 
     End With 

     With PvtTbl.PivotFields("Business Name") 
      .Orientation = xlRowField 
      .Position = 5 
     End With 

     With PvtTbl.PivotFields("Annual Aggregate Volume") 
      .Orientation = xlValuesField 
      .Position = 1 
     End With 

     PvtTbl.AddDataField PvtTbl.PivotFields("Annual Aggregate Volume"), 
      "Sum of Annual Aggregate Volume", xlSum 

     With PvtTbl.PivotFields("Sum of Annual Aggregate Volume") 
      .NumberFormat = "#,##0" 
     End With 

    Else 
     ' just refresh the Pivot cache with the updated Range 
     PvtTbl.ChangePivotCache PvtCache 
     PvtTbl.RefreshTable 
    End If 

    End Sub 
+0

何行エラーがで発生しますか?マクロの始まりはどこですか? – BruceWayne

+0

こんにちはBruceWayne、エラーはコードの最初のセクションで始まります。これはマクロが始まる場所です。このマクロを記録し、自分のデータにレコードを実装しようとしました。それは私にそのエラーを与える。 –

+0

@ J.Woeあなたは私の答えでコードをテストしましたか?あなたが意図したようにそれはあなたのために働いていますか? –

答えて

0

Option Explicit 

Sub SutoPivot() 

Dim PvtTbl       As PivotTable 
Dim PvtCache      As PivotCache 

' set the Pivot Cache (NOT SURE what is "TableX" ?) 
Set PvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="TableX", Version:=xlPivotTableVersion14) 

' add this line in case the Pivot table doesn't exit >> first time running this Macro 
On Error Resume Next 
Set PvtTbl = Worksheets("Invoice Data").PivotTables("PivotTable1a") 

On Error GoTo 0 
If PvtTbl Is Nothing Then 
    ' create a new Pivot Table in "Invoice Data" sheet, start from Cell Y1 
    Set PvtTbl = shtReport.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=Worksheets("Invoice Data").Range("Y1"), TableName:="PivotTable1a") 

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

    With PvtTbl.PivotFields("Manager") 
     .Orientation = xlRowField 
     .Position = 2 
    End With 

    With PvtTbl.PivotFields("Owner") 
     .Orientation = xlRowField 
     .Position = 3 
    End With 

    With PvtTbl.PivotFields("Account Name") 
     .Orientation = xlRowField 
     .Position = 4 
    End With 

    With PvtTbl.PivotFields("Business Name") 
     .Orientation = xlRowField 
     .Position = 5 
    End With 

    PvtTbl.AddDataField PvtTbl.PivotFields("Annual Aggregate Volume"), _ 
     "Sum of Annual Aggregate Volume", xlSum 

    With PvtTbl.PivotFields("Sum of Annual Aggregate Volume") 
     .NumberFormat = "#,##0" 
    End With 

Else 
    ' just refresh the Pivot cache with the updated Range 
    PvtTbl.ChangePivotCache PvtCache 
    PvtTbl.RefreshTable 
End If 

End Sub 
関連する問題