2017-04-13 15 views
0

VBAを使用してピボットテーブルを作成しようとしていますが、空のピボットテーブルを作成できるように@ Shai Radoのヘルプがあり、私は再びそれを実行しようとするとエラーが発生します:オブジェクト変数またはブロック変数が "コードのため"に設定されていない.PivotFields( "MedID") "Excel VBAランタイムエラー '91'

これは私にとって意味がありません。コードはすでに空のピボットテーブルを作成し、今ではエラーを示します。

任意の考えは?私はVBAに全く新しいですので、私は多くの「愚かな」質問がある。私は本当に任意の助けに感謝!!

Option Explicit 

Sub Create_Pivot_Table() 

Dim wsData As Worksheet, wsPT As Worksheet 
Dim PT_Cache As PivotCache 
Dim PT   As PivotTable 
Dim PRng  As Range 
Dim LastRow  As Long 

With ThisWorkbook 
    Set wsData = .Worksheets("Data") 
    Set wsPT = .Worksheets("Pivot Table") 
End With 

LastRow = wsData.Cells(wsData.Rows.Count, 1).End(xlUp).Row 
MsgBox LastRow ' <-- confirm value 

Set PRng = wsData.Range("A1:O" & LastRow) 

' option 2: Set the Pivot Cache 
Set PT_Cache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRng.Address(True, True, xlR1C1, True)) 

' set the Pivot Table 
Set PT = PT_Cache.CreatePivotTable(wsPT.Range("D5"), "Pivot_Table_Test") 

With PT 



    With .PivotFields("MedID") 
     .Orientation = xlRowField 
     .Position = 1 
     .LayoutBlankLine = False 
     .Subtotals(1) = False 
    End With 

    With .PivotFields("TransactionType") 
     .Orientation = xlColumnField 
     .Position = 1 
     .LayoutBlankLine = False 
     .Subtotals(1) = False 

    End With 


    With .PivotFields("Quantity") 
     .Orientation = xlDataField 
     .Function = xlCount 
     .Position = 1 

    End With 

Set PT = Nothing 
Set PT_Cache = Nothing 
Set wsData = Nothing 
Set wsPT = Nothing 
Exit Sub 

End With 

End Sub 
+0

は思えます。 Set PT = CreatePivotTable(....が値を返していて、PTを設定しています。 – aguertin

+0

コードの最初の行(存在する場合)としてピボット・テーブルを削除してみてください。 – aguertin

+0

@aguertin 'PivotTable'が存在すればそれを削除する必要はなく、そうであるかどうかを確認するだけです。必要なのは更新された' PivotCache'で更新するだけです'PivotTable'を追加します。 –

答えて

0

のような名前を使用して、ピボットテーブルにPTを参照してください:オブジェクトPTはnullであるよう

Option Explicit 

Sub Create_Pivot_Table() 

Dim wsData As Worksheet, wsPT As Worksheet 
Dim PT_Cache As PivotCache 
Dim PT   As PivotTable 
Dim PRng  As Range 
Dim LastRow  As Long 

With ThisWorkbook 
    Set wsData = .Worksheets("Data") 
    Set wsPT = .Worksheets("Pivot Table") 
End With 

LastRow = wsData.Cells(wsData.Rows.Count, 1).End(xlUp).Row  
Set PRng = wsData.Range("A1:O" & LastRow) 

' Set the Pivot Cache 
Set PT_Cache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRng.Address(True, True, xlR1C1, True)) 

' add this line in case the Pivot table doesn't exit >> first time running this Macro 
On Error Resume Next 
Set PT = wsPT.PivotTables("Pivot_Table_Test") ' check if "Pivot_Table_Test" Pivot Table already created (in past runs of this Macro) 

On Error GoTo 0 
If PT Is Nothing Then 
    ' create a new Pivot Table in "Pivot Table" sheet, start from Cell D5 
    Set PT = wsPT.PivotTables.Add(PivotCache:=PT_Cache, TableDestination:=wsPT.Range("D5"), TableName:="Pivot_Table_Test") 

Else ' just refresh the Pivot cache with the updated Range in "Data" sheet 
    PT.ChangePivotCache PT_Cache 
    PT.RefreshTable 
End If 

' === set Pivot Table fields === 
With PT 
    With .PivotFields("MedID") 
     .Orientation = xlRowField 
     .Position = 1 
     .LayoutBlankLine = False 
     .Subtotals(1) = False 
    End With 
    With .PivotFields("TransactionType") 
     .Orientation = xlColumnField 
     .Position = 1 
     .LayoutBlankLine = False 
     .Subtotals(1) = False 
    End With 
    With .PivotFields("Quantity") 
     .Orientation = xlDataField 
     .Function = xlCount 
     .Position = 1 
    End With 
End With 

Set PT = Nothing 
Set PT_Cache = Nothing 
Set wsData = Nothing 
Set wsPT = Nothing 
Exit Sub 

End Sub  
+0

これは完全に動作します!ありがとう! –

0

このエラーでは、PTがピボットテーブルに設定されていないように見えるので、With .PivotFields("MedID")のようには何も参照しません。 PTを作成し、(あなたのニーズに合わせて)の下に更新されたコードを試してみてくださいSet PT = ThisWorkbook.PivotTables("PivotName")

関連する問題