I have put together some VBA for a pivot table to basically just give how many CNs there are per person from the original table. Maybe there is a more simple way to do this, but I have to have code as I plan on adding this code to a macro. I have added a photo of my original table as well as what I am trying to have my pivot table look similar to. I get an error message on the [ With ws.PivotTables("PivotTable1").PivotFields("Type")] line: Unable to get the PivotFields property of the PivotTable class. Any ideas?
Example Table/pivot table photo Thanks!
Sub PivotTableTest2()
Dim ws As Worksheet
Dim wb As Workbook
Dim pc As PivotCache
Dim pt As PivotTable
Sheets("Supplier Quality").Activate
Set ws = ActiveSheet
Set wb = ThisWorkbook
Set pc = wb.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=ws.Range("Table2"))
Set pt = ws.PivotTables.Add(PivotCache:=pc, tabledestination:=ws.Range("P1"), TableName:="PivotTable1")
'
With ws.PivotTables("PivotTable1").PivotFields("Type")
.Orientation = xlRowField
.Position = 1
End With
With ws.PivotTables("PivotTable1").PivotFields("Task Owner2")
.Orientation = xlColumnField
.Position = 1
End With
ws.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Type"), "Sum of Tasks Overdue", xlSum
End Sub
very simple pivot table
is not created using vba, it is created directly in Excel. – LuukRange("P1")
should bews.Range("P1")
– Tim Williams