Unable to get the PivotFields property of the PivotTable class

ashish002

New Member
Joined
Jul 5, 2021
Messages
23
Office Version
  1. 365
Platform
  1. Windows
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.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
You are looping through every sheet and the code then assumes that every sheet that has at least 1 table on it will have the 1st table with columns with the labels brand, date and score. Is that actually the case ?
I can only get that error if the table does not have matching column heading in List Object 1 or the worksheet being processed.

To add a field with Max of Score add the line in red in the below:
Rich (BB code):
    For Each PivT In ws.PivotTables
        PivT.PivotFields("brands").Orientation = xlRowField
        PivT.PivotFields("date").Orientation = xlColumnField
        PivT.PivotFields("score").Orientation = xlDataField
        ' Add Max of Score field
        PivT.AddDataField PivT.PivotFields("score"), "Max of score", xlMax
    Next PivT
 
Upvote 0
Solution
You are looping through every sheet and the code then assumes that every sheet that has at least 1 table on it will have the 1st table with columns with the labels brand, date and score. Is that actually the case ?
I can only get that error if the table does not have matching column heading in List Object 1 or the worksheet being processed.

To add a field with Max of Score add the line in red in the below:
Rich (BB code):
    For Each PivT In ws.PivotTables
        PivT.PivotFields("brands").Orientation = xlRowField
        PivT.PivotFields("date").Orientation = xlColumnField
        PivT.PivotFields("score").Orientation = xlDataField
        ' Add Max of Score field
        PivT.AddDataField PivT.PivotFields("score"), "Max of score", xlMax
    Next PivT
Yes, the code has to loop through each sheet and create pivot table for the mentioned 3 columns. I ran this in another workbook and it worked perfect along with the code for max of score. Is something wrong with the code, which breaks it when i run it in another workbook?

Also, is there any way (vba/macro) to copy only the pivot tables created in each sheet and paste into a new workbook (separate sheet)?

Thanks again for your time.
 
Upvote 0
It relies on each sheet that has at least 1 table on it, that the 1st table on the sheet has those exact field names.
 
Upvote 0
I have logged off for the night. If you start a new thread since it a new topic, someone might come back to you in your time zone. If you leave a link to the new thread here, I will have a look tomorrow if it hasn't been answered yet.
 
Upvote 0
Thanks for your help. Much Appreciated. Here's the link to the new thread.

 
Upvote 0
Hi Alex,

Do you see any error in this code? I received response on the other thread but it doesn't seem to work. I am getting runtime error 91 "Object variable or With block variable not set". Please help.

Sub CopySheets()

Dim wb As Workbook, nam As String, i As Long
Dim twb As Workbook: Set twb = ThisWorkbook
Dim ws As Object: Set ws = Worksheets

Application.ScreenUpdating = False
nam = twb.Name
Set wb = Workbooks.Add
twb.Activate
For i = 1 To ws.Count
ws(i).Range("W2:BB1000").Copy wb.ActiveSheet.Range("W2")
wb.ActiveSheet.Name = ws(i).Name
If i = ws.Count Then
twb.Activate
Application.ScreenUpdating = True
MsgBox "Operation Complete"
Exit Sub
End If
wb.ActiveSheet.Next.Activate
Next
MsgBox "Operation Complete"
Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,215
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top