Hi All,
Facing issue with creating pivot table in loop. It's giving error: "Unable to get the PivotFields property of the PivotTable class"
Here's the code:
Sub PivotTableLoop()
Dim ws As Worksheet
Dim PivC As PivotCache
Dim PivT As PivotTable
Dim Table As ListObject
For Each ws In ActiveWorkbook.Worksheets
Set PivC = Nothing
Set PivT = Nothing
Set Table = Nothing
On Error Resume Next
' Set PivotTable Cache
Set PivC = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:=ws.ListObjects(1), Version:=xlPivotTableVersion15)
' Create the PivotTable
Set PivT = ws.PivotTables.Add(PivotCache:=PivC, _
TableDestination:=ws.Range("w2"), TableName:="PivotTable" & ws.Index)
On Error GoTo 0
For Each PivT In ws.PivotTables
PivT.PivotFields("brands").Orientation = xlRowField
PivT.PivotFields("date").Orientation = xlColumnField
PivT.PivotFields("score").Orientation = xlDataField
Next PivT
Next ws
End Sub
I also want to add a function to summarize the score by Max (xlMax) to the above code. Any help would be appreciated.
Facing issue with creating pivot table in loop. It's giving error: "Unable to get the PivotFields property of the PivotTable class"
Here's the code:
Sub PivotTableLoop()
Dim ws As Worksheet
Dim PivC As PivotCache
Dim PivT As PivotTable
Dim Table As ListObject
For Each ws In ActiveWorkbook.Worksheets
Set PivC = Nothing
Set PivT = Nothing
Set Table = Nothing
On Error Resume Next
' Set PivotTable Cache
Set PivC = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:=ws.ListObjects(1), Version:=xlPivotTableVersion15)
' Create the PivotTable
Set PivT = ws.PivotTables.Add(PivotCache:=PivC, _
TableDestination:=ws.Range("w2"), TableName:="PivotTable" & ws.Index)
On Error GoTo 0
For Each PivT In ws.PivotTables
PivT.PivotFields("brands").Orientation = xlRowField
PivT.PivotFields("date").Orientation = xlColumnField
PivT.PivotFields("score").Orientation = xlDataField
Next PivT
Next ws
End Sub
I also want to add a function to summarize the score by Max (xlMax) to the above code. Any help would be appreciated.