How to make my pivot fields dynamic in vba code

2k Views Asked by At

I have a pivot table that sorts data for various people for each month over a 12-month period. The table that the data is generated from has a dynamic date range (ie the user can specify the start date which will cascade through the table). The pivot table and chart is generated from this master table as part of the data analysis. This worked fantastic last month when I created this tool, however I realised in my code I have hard-coded names of the pivot fields to correspond to months from April (please see below). This makes my pivot table break when I try to run it from a start date in May. How can I make the "PivotFields"/data fields in the pivot table dynamic to correspond to the date range?

Relevant piece of the code found below. As one would expect, the error happens at:

    "Compare individuals").PivotFields("Apr-17"), "Sum of Apr-17", xlSum
ActiveSheet.PivotTables("Compare individuals").AddDataField 

When trying to run my pivot chart/report from May.

Really appreciate any pointers!

Option Explicit
Sub CreatePivot()
 Dim pt          As PivotTable
 Dim pf          As PivotField
 Dim pi          As PivotItem
 Dim pc          As PivotCache

  With ActiveWorkbook
    For Each pc In .PivotCaches
        pc.MissingItemsLimit = xlMissingItemsNone
    Next pc
  End With


    'Generate base pivot chart
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Indy", Version:=xlPivotTableVersion10). _
        CreatePivotTable TableDestination:="'PivotSheet'!R8C6", TableName:="Compare individuals" _
        , DefaultVersion:=xlPivotTableVersion10

    Worksheets("PivotSheet").Activate
    'Set Name and disc as field and filter respectively
    With ActiveSheet.PivotTables("Compare individuals").PivotFields("Name")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("Compare individuals").PivotFields("Disc ")
        .Orientation = xlPageField
        .Position = 1
    End With

    'Add data; sums of each month per individual
    ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
        "Compare individuals").PivotFields("Apr-17"), "Sum of Apr-17", xlSum
    ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
        "Compare individuals").PivotFields("May-17"), "Sum of May-17", xlSum
    ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
        "Compare individuals").PivotFields("Jun-17"), "Sum of Jun-17", xlSum
    ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
        "Compare individuals").PivotFields("Jul-17"), "Sum of Jul-17", xlSum
    ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
        "Compare individuals").PivotFields("Aug-17"), "Sum of Aug-17", xlSum
    ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
        "Compare individuals").PivotFields("Sep-17"), "Sum of Sep-17", xlSum
    ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
        "Compare individuals").PivotFields("Oct-17"), "Sum of Oct-17", xlSum
    ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
        "Compare individuals").PivotFields("Nov-17"), "Sum of Nov-17", xlSum
    ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
        "Compare individuals").PivotFields("Dec-17"), "Sum of Dec-17", xlSum
    ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
        "Compare individuals").PivotFields("Jan-18"), "Sum of Jan-18", xlSum
    ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
        "Compare individuals").PivotFields("Feb-18"), "Sum of Feb-18", xlSum
    ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
        "Compare individuals").PivotFields("Mar-18"), "Sum of Mar-18", xlSum
    ActiveSheet.PivotTables("Compare individuals").PivotFields("Disc ").CurrentPage = _
        "(All)"
'Set data in columns
    With ActiveSheet.PivotTables("Compare individuals").DataPivotField
        .Orientation = xlColumnField
        .Position = 1
    End With

EDIT: a bit more context. My PivotTable looks like this: Pivot

The error ostensibly shows up because there's no "Apr-17" in the actual data table

My source data looks like this: Source

Wherein the months/date range are dynamic (from initial input).

What I want the code to do is this: Desired

Ie put all the date columns into the table regardless of what they are called.

1

There are 1 best solutions below

1
On BEST ANSWER

@A.Doe, give this a try

Dim f1 As PivotField
For Each f1 In ActiveSheet.PivotTables("Compare individuals").PivotFields
    'MsgBox f1.Name test code
    If (InStr(f1.Name, "17") > 0) Then 'or some valid condition
        ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
            "Compare individuals").PivotFields(f1.Name), "Sum of " & f1.Name, xlSum
    End If
Next