Setting a rolling month on the date filter of a Pivot Table

adaircameron

New Member
Joined
Mar 30, 2011
Messages
7
Hi,

I have been trying to figure out a way of setting a rolling month/year on the date filter of a pivot table, can someone advise?

I read on one blog that a set needs to be created from with the 'Options' tab of the 'PivotTable Tools'. Unfortunately, this is always disabled so cannot select any of the following from the 'Fields, ITems, & Sets' dropdown:

  • Create set based on row items
  • Create set based on column items
  • Manage sets

Is there any other ways of doing this?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
This may be a little closer to what you need. It will still require some modification:
Code:
Sub MakeCurrentMontyAndYearVisibleInPivotTable()
'
    Dim iX As Integer
    Dim bFound As Boolean
 
    Range("A4").Select 'Select Date Header in Pivot Table
    Selection.Group Start:=True, End:=True, Periods:=Array(False, False, False, _
        False, True, False, True) 'Group by Months & Years
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Years")
        bFound = False
        For iX = 1 To ActiveSheet.PivotTables("PivotTable1").PivotFields("Years").PivotItems.Count
            If ActiveSheet.PivotTables("PivotTable1").PivotFields("Years").PivotItems(iX).Name = CStr(Year(Now())) Then
                bFound = True 'Current Year data is present
                Exit For
            End If
        Next
        If bFound = True Then
            'make current year pivotitem visible
            .PivotItems(CStr(Year(Now()))).Visible = True
            'Hide all the others
            For iX = 1 To ActiveSheet.PivotTables("PivotTable1").PivotFields("Years").PivotItems.Count
                Select Case ActiveSheet.PivotTables("PivotTable1").PivotFields("Years").PivotItems(iX).Name
                Case CStr(Year(Now()))
                    'Already made visible = do nothing
                Case Else
                    .PivotItems(iX).Visible = False 'hide it
                End Select
            Next
        Else
            MsgBox "No data for " & Year(Now()) & " is present"
            GoTo End_Sub
        End If
    End With
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("date1") 'date1 was the name of my date field
                                                                     'I am not sure why this was not "Months"
        bFound = False
        For iX = 1 To ActiveSheet.PivotTables("PivotTable1").PivotFields("date1").PivotItems.Count
            If ActiveSheet.PivotTables("PivotTable1").PivotFields("date1").PivotItems(iX).Name = Format(Now(), "mmm") Then
                bFound = True 'Current Year data is present
                Exit For
            End If
        Next
        If bFound = True Then
            'make current year pivotitem visible
            .PivotItems(Format(Now(), "mmm")).Visible = True
            'Hide all the others
            For iX = 1 To ActiveSheet.PivotTables("PivotTable1").PivotFields("date1").PivotItems.Count
                Select Case ActiveSheet.PivotTables("PivotTable1").PivotFields("date1").PivotItems(iX).Name
                Case Format(Now(), "mmm")
                    'Already made visible = do nothing
                Case Else
                    .PivotItems(iX).Visible = False 'hide it
                End Select
            Next
        Else
            MsgBox "No data for " & Format(Now(), "mmm") & " is present"
            GoTo End_Sub
        End If
    End With
 
End_Sub:
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

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