I have a pivot table with a pivot field of Week Date.
My vba tries to hide all of the week dates except for the current week date. It gets that current week date value from a cell in the sheet. It works fine and hides every date that is not the current week date until it gets to 1 from the end, and then errors with a 1004, unable to set visible class. Any ideas? Code is listed below. Thanks!
I've stepped through the code and when it gets to the date it will error on, it recognizes that it is in fact the Else, then goes to pi.visible = False, but then errors when it tries to set the property.
My vba tries to hide all of the week dates except for the current week date. It gets that current week date value from a cell in the sheet. It works fine and hides every date that is not the current week date until it gets to 1 from the end, and then errors with a 1004, unable to set visible class. Any ideas? Code is listed below. Thanks!
Code:
For Each w In ThisWorkbook.Worksheets
For Each pt In w.PivotTables
pt.RefreshTable
pt.Update
Next
Next
For Each w In ThisWorkbook.Worksheets
For Each pt In w.PivotTables
Set pf = pt.PivotFields("Week Date")
pf.ClearAllFilters
For Each pi In pf.PivotItems
If pi = Worksheets("Control Sheet").Range("B10").Value Then
'pi.Visible = True
Else
pi.Visible = False
End If
Next pi
Next
Next
I've stepped through the code and when it gets to the date it will error on, it recognizes that it is in fact the Else, then goes to pi.visible = False, but then errors when it tries to set the property.