Johnny C
Well-known Member
- Joined
- Nov 7, 2006
- Messages
- 1,069
- Office Version
- 365
- Platform
- Windows
I've got a workbook with a ton of pivot tables, most of which have a page filter based on Month. I want to be able to enter a number in a cell, then run a macro to go through all pivots and 'tick' the value in the list of month values so that the month is included.
This is the code I have. The 3 lines with Visible = True are me trying to get it done diffrent ways. They all give the same error "Unable to set the visible item of the pivotItem class".
What am I doing wrong? 6 pages of Googles say this is what you do.
I even tried hard coding it and that didn't work
End Sub
Thanks
This is the code I have. The 3 lines with Visible = True are me trying to get it done diffrent ways. They all give the same error "Unable to set the visible item of the pivotItem class".
What am I doing wrong? 6 pages of Googles say this is what you do.
VBA Code:
Dim ws As Worksheet
Dim pt As PivotTable
Dim pi As PivotItem
Dim pf As PivotField
Dim IntLatestMonth%, strLatestMonth$
On Error Resume Next
IntLatestMonth = wksData.Range("LatestMonth").Value
If Err.Number <> 0 Then IntLatestMonth = 0
On Error GoTo 0
strLatestMonth$ = CStr(IntLatestMonth)
For Each ws In ThisWorkbook.Worksheets
For Each pt In ws.PivotTables
Application.StatusBar = "Refreshing tab [" & ws.Name & "] Pivot table " & pt.Name
On Error Resume Next
pt.RefreshTable
On Error GoTo 0
For Each pf In pt.PageFields
If pf.Name = "Month" Then
pf.PivotItems(strLatestMonth).Visible = True
For Each pi In pf.PivotItems
If pi.Name = strLatestMonth Then pi.Visible = True
If pi.Name = strLatestMonth Then pf.PivotItems(strLatestMonth).Visible = True
Next pi
End If
Next pf
On Error GoTo 0
Next
Next
I even tried hard coding it and that didn't work
VBA Code:
Sheets("Sales Summary (1)").PivotTables("PivotTable1").PivotFields("Month").PivotItems("4").Visible = True
End Sub
Thanks
Last edited: