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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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