Updating Pivot Table Date Field

elevate_yourself

New Member
Joined
Oct 9, 2014
Messages
15
Hi All,

Im trying to work on a VBA script for updating a pivot table date field based on a reference cell value and am running into some issues.

The code below is what im working with. Right now when I run this I get an "Application-defined or object-defined error" with the red text below.
Any help would be appreciated!

Code:
Sub RefreshPivot()
    
    Dim pt As PivotTable
    Dim ValDate As Date
    'This macro will Refresh ODS Pivots


    StartTime = Now()
    Application.ScreenUpdating = False
    'Create string for pivot field date selection
    dtVal_Date = Sheets("Sheet1").Range("ValDate").Value
    dtyear = Format(Year(dtVal_Date), "0000")
    dtMonth = Format(Month(dtVal_Date), "00")
    dtday = Format(Day(dtVal_Date), "00")
    dtDate = dtyear & "-" & dtMonth & "-" & dtday
    strDate_Filter = "[System Date].[Date Hierarchy].&[" & dtDate & "T00:00:00]"


    
    For Each pt In Sheets("Sheet1").PivotTables
        With pt.PivotFields("[System Date].[Date Hierarchy].[Date]")
[COLOR=#ff0000][FONT=arial black]            .CurrentPageName = strDate_Filter[/FONT][/COLOR]
        End With
    Next pt
 
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Managed to get this working. The issue was with how the pivot field was defined. Posting final code just for reference.

Code:
Sub RefreshPivot()
    
    Dim pt As PivotTable
    Dim ValDate As Date




    StartTime = Now()
    Application.ScreenUpdating = False
    'Create string for pivot field date selection
    dtVal_Date = Sheets("Sheet1").Range("ValDate").Value
    dtyear = Format(Year(dtVal_Date), "0000")
    dtMonth = Format(Month(dtVal_Date), "00")
    dtday = Format(Day(dtVal_Date), "00")
    dtDate = dtyear & "-" & dtMonth & "-" & dtday
    strDate_Filter = "[System Date].[Date Hierarchy].[Date].&[" & dtDate & "T00:00:00]"
    'strDate_Filter = "[System Date].[Date Hierarchy].&[" & dtDate & "]"
    
       
    For Each pt In Sheets("Sheet1").PivotTables
        With pt.PivotFields("[System Date].[Date Hierarchy].[Year]")
            .CurrentPageName = strDate_Filter
        End With
    Next pt
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,172
Messages
6,129,291
Members
449,498
Latest member
Lee_ray

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