[VBA] Adding pivot fields with VBA

PipBoy808

Board Regular
Joined
Oct 30, 2013
Messages
107
I use the code below to generate a pivot table:

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 :)
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I usually get more joy creating the pivot from a data range, and then hiding fields i don't want to see whilst recording in macro, then tidying that
 
Upvote 0
I usually get more joy creating the pivot from a data range, and then hiding fields i don't want to see whilst recording in macro, then tidying that

The problem is that the data range is dynamic. It's imported from a different file every single time and is effectively 'reset'. As a result, I think I'm going to have to clear any existing pivots and generate a new one every time. So, the whole process needs to be in the code.
 
Upvote 0
Have you tried adding fields before setting the field details?

An example:

Code:
Activesheet.PivotTables("MyTable").AddFields RowFields:=Array("Year", "Month"), ColumnFields:="Data"

Hope this helps.


Tim
 
Upvote 0
OK, i used dynamic length pivots that i rebuild on update, as long as the header names never change then recording and modifying works great for me
 
Upvote 0
I ended up recording a macro of myself setting up the pivot, and replacing the source data range with a dynamic variable that is reset every time the macro runs, setting up a new pivot each time. Works like a charm!
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,150
Members
448,552
Latest member
WORKINGWITHNOLEADER

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