Cubist
Well-known Member
- Joined
- Oct 5, 2023
- Messages
- 2,135
- Office Version
- 365
- Platform
- Windows
- MacOS
Hi,
I have a toggle on onan Excel sheet that toggles between 2 options: "Plan YTD" or "Rolling 12". The Slicer and the pivot tables would update based on the selection between the 2. It working but not at an optimal speed. I'm wondering if there's a way to speed this up. Thanks.
I have a toggle on onan Excel sheet that toggles between 2 options: "Plan YTD" or "Rolling 12". The Slicer and the pivot tables would update based on the selection between the 2. It working but not at an optimal speed. I'm wondering if there's a way to speed this up. Thanks.
VBA Code:
Sub Plan_Rolling()
Application.ScreenUpdating = False
Application.EnableEvents = False
On Error Resume Next
Dim pt As PivotTable
Dim pivotTableOrder As Variant
Dim pivotTableName As String
Dim i As Long
If Details.Range("PlanRollingSwitch") = "Plan YTD" Then 'make it R12
Details.Range("PlanRollingSwitch") = "*Rolling 12"
Details.Shapes("Rolling12Slicer").Visible = msoTrue
pivotTableOrder = Array("Pivot_All_Budget", "Pivot_All_Contribs", _
"Pivot_Medical_Budget", "Pivot_Medical_Contribs", "Pivot_Medical_Claims", _
"Pivot_Medical_ASL", "Pivot_Medical_Fixed", "Pivot_Medical_Enroll", _
"Pivot_Dental_Budget", "Pivot_Dental_Enroll", _
"Pivot_Vision_Budget", "Pivot_Vision_Enroll")
For i = LBound(pivotTableOrder) To UBound(pivotTableOrder)
pivotTableName = pivotTableOrder(i)
Set pt = Details.PivotTables(pivotTableName)
With pt
.PivotFields("Plan Year").Orientation = xlHidden
.PivotFields("Rolling 12").Orientation = xlRowField
.PivotFields("Rolling 12").Position = 1
.PivotFields("Rolling 12").AutoSort xlDescending, "Rolling 12"
.PivotFields("Month").AutoSort xlAscending, "Month"
End With
'Clean up
Set pt = Nothing
pivotTableName = vbNullString
Next i
Else
Details.Range("PlanRollingSwitch") = "Plan YTD"
pivotTableOrder = Array("Pivot_All_Budget", "Pivot_All_Contribs", _
"Pivot_Medical_Budget", "Pivot_Medical_Contribs", "Pivot_Medical_Claims", _
"Pivot_Medical_ASL", "Pivot_Medical_Fixed", "Pivot_Medical_Enroll", _
"Pivot_Dental_Budget", "Pivot_Dental_Enroll", _
"Pivot_Vision_Budget", "Pivot_Vision_Enroll")
For i = LBound(pivotTableOrder) To UBound(pivotTableOrder)
pivotTableName = pivotTableOrder(i)
Set pt = Details.PivotTables(pivotTableName)
With pt
.PivotFields("Rolling 12").Orientation = xlHidden
.PivotFields("Plan Year").Orientation = xlRowField
.PivotFields("Plan Year").Position = 1
.PivotFields("Plan Year").AutoSort xlAscending, "Plan Year"
.PivotFields("Month").AutoSort xlAscending, "Month"
End With
'Clean up
Set pt = Nothing
pivotTableName = vbNullString
Next i
End If
On Error GoTo 0
'ErrorHandlerExit:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub