Hi All, i currently have the below code which works perfectly on one of my pivots (pivot has several data columns - a Value for each column in field settings) which sorts based on cell selection.
However this code doesn't work for my other pivot which has multiple columns of data but comes from only one value in the field settings. is there any reason why this wont sort? thanks
However this code doesn't work for my other pivot which has multiple columns of data but comes from only one value in the field settings. is there any reason why this wont sort? thanks
VBA Code:
Sub Pivot_Sort()
Application.DisplayAlerts = False
'select the Value field that
' the sort will be based on
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