VBA Help for PivotTable Filter Update

nsfekeci

New Member
Joined
Dec 27, 2018
Messages
1
Hi,

This should be a fairly easy problem to fix, but I'm trying to update the date filters in a series of pivots using a named range within excel. My code is below and works fine as is, but I'm trying to replace the hardcoded date parameter in the last line with StartDate, which takes a date from the "Starting_Month" named range. This will always be set to a valid date.


Code:
Private Sub UpdateDateRange()
'
' Update Date Range for Pivot Slicers and Tables
'
Dim StartDate As Date
Dim Pivot_sht As Variant


Set Pivot_sht = ThisWorkbook.Worksheets("Pivots_HCMovement")
StartDate = ThisWorkbook.Worksheets("Summary_HCMovement").Range("Starting_Month")


    Pivot_sht.PivotTables("Start_Summary").PivotFields( _
        "[Headcount Data].[Report Date].[Report Date]").VisibleItemsList = Array( _
        "[Headcount Data].[Report Date].&[2018-04-30T00:00:00]")

End Sub

When I replace [2018-04-30T00:00:00] with StartDate, it gives me this error:

"Run -time error '1004': The query did not run or the Data Model could not be accessed. Here's the error message we got: The 'StartDate' string cannot be converted to the date type."

What's the correct way to take the string as it appears in the "Starting_Month" named range and convert it into the Data Model date format so I can use excel to change the filters automatically.

Code:
Private Sub UpdateDateRange()
'
' Update Date Range for Pivot Slicers and Tables
'
Dim StartDate As Date
Dim Pivot_sht As Variant


Set Pivot_sht = ThisWorkbook.Worksheets("Pivots_HCMovement")
StartDate = ThisWorkbook.Worksheets("Summary_HCMovement").Range("Starting_Month")


    Pivot_sht.PivotTables("Start_Summary").PivotFields( _
        "[Headcount Data].[Report Date].[Report Date]").VisibleItemsList = Array( _
        "[Headcount Data].[Report Date].&StartDate")

End Sub

Thanks for the help,

Nick
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Forum statistics

Threads
1,215,731
Messages
6,126,538
Members
449,316
Latest member
sravya

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