[VBA] Adding pivot fields with VBA

PipBoy808

Board Regular
Joined
Oct 30, 2013
Messages
106
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 :)
 

Some videos you may like

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

mole999

Moderator
Joined
Oct 23, 2004
Messages
10,524
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
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
 

PipBoy808

Board Regular
Joined
Oct 30, 2013
Messages
106
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.
 

timfritsch

Active Member
Joined
Jun 10, 2004
Messages
343
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
 

mole999

Moderator
Joined
Oct 23, 2004
Messages
10,524
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
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
 

PipBoy808

Board Regular
Joined
Oct 30, 2013
Messages
106
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!
 

Watch MrExcel Video

Forum statistics

Threads
1,122,962
Messages
5,599,065
Members
414,281
Latest member
Engjamal2021

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
Top