I use the code below to generate a pivot table:
However, I'm not sure how to add fields. I found the following code, but it keeps returning errors:
Can anyone help me out? I'm looking to add the field 'Stock Id' to rows and sum of 'Total Stock' to the values part of the field list.
Any help would be much appreciated. I'd be lost without this forum! Thanks
Code:
'Create the Cache
Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:=DataRange)
PivotSheet.Select
'Create the Pivot table
Set PT = ActiveSheet.PivotTables.Add(PivotCache:=PTCache, _
TableDestination:=Range("A6"), TableName:="PGPivot")
ActiveWorkbook.ShowPivotTableFieldList = True
'Adding fields
??????????????????
However, I'm not sure how to add fields. I found the following code, but it keeps returning errors:
Code:
With .PivotFields("Stock Id") 'returns "Object variable or With block variable not set" error
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Total Stock")
.Orientation = xlDataField
.Position = 1
.Caption = "Total Stock"
.Function = xlSum
End With
Can anyone help me out? I'm looking to add the field 'Stock Id' to rows and sum of 'Total Stock' to the values part of the field list.
Any help would be much appreciated. I'd be lost without this forum! Thanks