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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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