vbapivotpivotitem

VBA Referencing Dynamic PivotItem


This should be a simple question - how do I select the value of a pivot filter regardless of the actual cell it is in? In other words, I have a PivotTable with a report filter labelled 'Month', whose values go from January through to December. What code can I use to select the current month? Where do I go from this:

Dim Pt As PivotTable
Dim PtPageField As PivotField
Dim PtItem As PivotItem

Set Pt = Sheets("Pivot").PivotTables(1)
Set PtPageField = Pt.PageFields("Month")

I can't use simply Pt.PageRange.Select as this would select both the 'Month' cell and 'January' cell (for example). I also don't want to manually code that it selects 'January', 'February' etc and would like a more efficient solution that it selects whatever value is currently chosen.


Solution

  • You want to determine what value has been selected?

    PtPageField.DataRange.Value
    

    You want to set the value to the current month?

    ptPage.DataRange.Value = Format(Now, "mmmm")