VBA: Group Pivot Dates By Year

VickyW

New Member
Joined
Mar 1, 2020
Messages
6
Office Version
  1. 365
  2. 2013
  3. 2010
Platform
  1. Windows
Hi All

I am hoping someone can help

I'm trying to group a pivot table by year & month in my workbook, but getting the following error: 'Runtime error 1004 - Group method of range class failed'

I tried to follow the same principle as this MrExcel video, as I don't want to have to select or reference a specific cell in the code (in case the user inadvertently changes the pivot address by adding/deleting rows/columns)

But I can't seem to make it work.
Any help appreciated

Vicky


VBA Code:
Sub GroupPivotDate()

Dim pf As PivotField

Set pf = ActiveSheet.PivotTables("PivotTable1").PivotFields("Sale Date")
  
    pf.LabelRange.Group Start:=True, End:=True, Periods:=Array(False, False, False, _
        False, True, False, True)


End Sub

Example workbook
Group Pivot By Date.xlsm

Also asked here VBA: Group Pivot Dates By Year

Screen Shot.png
 
Last edited by a moderator:

Some videos you may like

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

VickyW

New Member
Joined
Mar 1, 2020
Messages
6
Office Version
  1. 365
  2. 2013
  3. 2010
Platform
  1. Windows
Solved in cross post. Thank you all
 

Watch MrExcel Video

Forum statistics

Threads
1,113,744
Messages
5,543,956
Members
410,586
Latest member
acadavid86
Top