Hi All, below is my current code i use to auto sort my pivot based on cell selected in the pivot. this previously worked on a pivot table but on a new workbook its randomly stopped working.
does anyone have an alternative code or can this code be simplified to work?
does anyone have an alternative code or can this code be simplified to work?
VBA Code:
Sub Pivotsort()
Application.DisplayAlerts = False
Dim pt As PivotTable
Dim pf As PivotField
Dim df As PivotField
Dim strVal As String
On Error Resume Next
Set pt = ActiveCell.PivotTable
If pt Is Nothing Then Exit Sub
Set df = ActiveCell.PivotField
If df.Orientation <> xlDataField Then
MsgBox "Please select a Values field"
Exit Sub
Else
If pt.PivotCache.OLAP = True Then
strVal = df.Name
Else
strVal = df.Caption
End If
End If
For Each pf In pt.RowFields
pf.AutoSort xlDescending, strVal
Next pf
Application.DisplayAlerts = True
End Sub