VBA: Group Pivot Dates By Year

VickyW

New Member
Joined
Mar 1, 2020
Messages
9
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:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,090
Latest member
vivek chauhan

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