My code below is used to close all the pivotfield in the sheet. As the data imported to Excel is very large, so this calculation is now causing my excel to lag. Is there a way to speed it up??
VBA Code:
Private Sub Worksheet_Deactivate()
Dim pf As PivotField
Dim pi As PivotItem
For Each pf In ThisWorkbook.Sheets("Sheet1").PivotTables("PivotTable4").PivotFields
For Each pi In pf.PivotItems
If pi.ShowDetail = True Then
Application.EnableEvents = False
pi.ShowDetail = False
Application.EnableEvents = True
End If
Next pi
Next pf
For Each pf In ThisWorkbook.Sheets("Sheet1").PivotTables("PivotTable2").PivotFields
For Each pi In pf.PivotItems
If pi.ShowDetail = True Then
Application.EnableEvents = False
pi.ShowDetail = False
Application.EnableEvents = True
End If
Next pi
Next pf
End Sub