I'm trying to write some code where it will select the max pivot item in "Workweek". However, I can't get it to quite work right. Thanks for your help.
Sub DW_Records_Setup()
'3 DW_Records_Setup
Dim wk4 As Worksheet
Set wk4 = ThisWorkbook.Worksheets("Pivot_Example")
Dim piItem As PivotItem
Dim lngMaxValue As Double
Set piItem = Nothing
'code below is for GetBook variable with all connections, rewrite later to a public variable
ThisWorkbook.ActiveSheet.PivotTables("PivotTable1").ClearTable
With wk4.PivotTables("PivotTable1")
.InGridDropZones = True
.RowAxisLayout xlTabularRow
End With
With wk4.PivotTables("PivotTable1").PivotFields("Workweek")
.Orientation = xlPageField
.Position = 1
End With
wk4.PivotTables("PivotTable1").AddDataField wk4.PivotTables( _
"PivotTable1").PivotFields("Sales"), "Sum of Sales", _
xlSum
wk4.PivotTables("PivotTable1").PivotFields("Workweek").ShowAllItems = False
'added code below this line for auto update
For Each piItem In wk4.PivotTables("PivotTable1").PivotFields("Workweek").PivotItems
If piItem.Value > lngMaxValue Then lngMaxValue = piItem.Value
With wk4.PivotTables("PivotTable1").PivotFields("Workweek") 'added with statement in the loop
.PivotItems(lngMaxValue).Visible = True
.PivotItems(piItem.Value).Visible = False
.EnableMultiplePageItems = True
End With
pvtcount = pvtcount + 1
Next piItem
Set piItem = Nothing
x = 1
With wk4.PivotTables("PivotTable1").PivotFields("Workweek") 'added with
For x = 1 To pvtcount
If .PivotItems(x).Value = lngMaxValue Then
.PivotItems(x).Visible = True
End If
Next
.PivotItems("14").Visible = False
.EnableMultiplePageItems = True
End With
End Sub
Bookmarks