I have tried many iterations that I have found on the web but cannot get this to work. I have a pivot table that I need to update weekly and I need the date to be set to a variable base on a field in the worksheet. The date is a filter. Using the code below, I can change the date to a specific date.
Sub Macro7()
'
ActiveSheet.PivotTables("PivotTable2").PivotFields("[Test_Data].[Date].[Date]" _
).ClearAllFilters
ActiveSheet.PivotTables("PivotTable2").PivotFields("[Test_Data].[Date].[Date]" _
).CurrentPageName = "[Test_Data].[Date].&[2020-07-12T00:00:00]"
End Sub
However, when I try to set the date to a variable such as a string that points to the worksheet cell containing the date, it errors out, normally stating 'Unable to set the CurrentPage property of the PivotField class'. I have tried many different options and different code but nothing has worked. D2 is where the date is that changes. It updates based on a formula. I have tried also just hard coding the dates in the cell but that does not work either. Have also tried matching up date formats to the pivot, did not help. The field pulls the date of the last Friday, if that matters. I would be fine putting that into the code instead, if that would help (I tried, I got an error that the string could not be converted to the date).
Sub My_macro()
Dim str As String
str = ActiveSheet.Range("d2")
ActiveSheet.PivotTables("PivotTable2").PivotFields("[Test_Data].[Date].[Date]" _
).ClearAllFilters
ActiveSheet.PivotTables("PivotTable2").PivotFields("[Test_Data].[Date].[Date]").CurrentPage = str
End Sub
Can someone please tell me how to correct this? Have been trying for hours and hours and just cannot get it figured. Thanks. PS--I tried to upload a sheet, but I cannot figure out how, looks like I can only do pictures, so I uploaded that. Below is the pivot where the date filter is the issue, and below that is a sample of what feeds the pivot.

Edit: Have shared a sample of the file here: https://www.dropbox.com/sh/4tzrrue0mggf6om/AAAd6024feSdc9MbCd9IZD0Wa?dl=0
