brawnystaff
Board Regular
- Joined
- Aug 9, 2012
- Messages
- 109
- Office Version
- 365
I use the macro below to sort a Pivot table descending based on the value cell I have selected in Pivot Table value section, regardless of how many Row Labels there are. It works for a standard Pivot Table, but not one that has been added to the Data Model. Any ideas as to what needs to be changed?
Code:
Sub SortAllRowFields_ZASelVal()
'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
MsgBox df
If df.Orientation <> xlDataField Then
MsgBox "Please select a Values field"
Exit Sub
Else
strVal = df.Caption
End If
For Each pf In pt.RowFields
pf.AutoSort xlDescending, strVal
Next pf
MsgBox "Row fields were sorted Z-A " _
& vbCrLf _
& "based on the Value field: " _
& vbCrLf _
& strVal
End Sub