VBA to select last 12 items in a pivot table drop down

nguerra

New Member
Joined
Oct 1, 2013
Messages
46
Looking for any kind of help that can help me solve this problem.

I receive a pivot table once a month. One of the filters has the options of:
yr17mth08,yr17mth09,yr17mth10,yr17mth11,yrmth12,
yr18mth01,yr18mth02,yr18mth03,yr18mth04,yr18mth05,yr18mth06,yt18mth07,yr17mth08,yr18mth09,yr18mth10,yr18mth11,yr18mth12,
yr19mth01,yr19mth02,yr19mth03,yr19mth04,yr19mth05,yr19mth06,yt19mth07.
however, every month the oldest field drops off and a new field is added. It's basically a rolling 24 months of data. The report generated monthly is only looking for the last 12 months. It's important to know that the values in the pivot table are not formatted as dates. I believe they are just strings.

I'm looking for a piece of VBA that can always select the last 12 items in a drop down no matter what they are.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Edit first Const line to contain the name of the pivotfield you want to filter.
Code:
Option Explicit

    Sub SubSelectLastXPTItems()
    'https://www.mrexcel.com/forum/excel-questions/1106946-vba-select-last-12-items-pivot-table-drop-down.html

    Const sPTField As String = "Year"
    Const lSelectCount As Long = 12
    
    Dim pt As PivotTable
    Dim ptf As PivotField
    Dim bFoundPTF As Boolean
    Dim pti As PivotItem
    Dim lPTICount As Long
    Dim lPTIIndex As Long
    
    'Make sure sPTField is a Pivot Field in the active PT
    With ActiveSheet.PivotTables(1)
        For Each ptf In .PivotFields
            If ptf.Name = sPTField Then
                bFoundPTF = True
                Exit For
            End If
        Next
        If bFoundPTF Then
            Set ptf = .PivotFields(sPTField)
            'How many items in that field?
            For Each pti In ptf.PivotItems
                lPTICount = lPTICount + 1
            Next
            ptf.ClearAllFilters
            If lPTICount > lSelectCount Then
                For lPTIIndex = 1 To lPTICount
                    If lPTICount - lPTIIndex >= lSelectCount Then
                        ptf.PivotItems(lPTIIndex).Visible = False
                    Else
                        ptf.PivotItems(lPTIIndex).Visible = True
                    End If
                Next
            Else
                MsgBox "There " & lSelectCount & " or fewer items in the " & sPTField & " pivotfield.  All will be displayed"
            End If
        Else
            MsgBox sPTField & " is not a pivotfield in the active pivottable."
        End If
    End With
End Sub
 
Upvote 0
Thank you for the reply. The first Const in my case is:

Const sPTField As String = "[CubeData].[Month].[Month]"

When I run the macro I get the following:

Run-time error '1004':
Unable to set the Visable property of the PivotItem class

ptf.PivotItems(lPTIIndex).Visible = False
 
Upvote 0
If it was a regular pivot table "Month" would do. I am not sure about the CubeData aspect of it.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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