Hi All,
I currently have the below code to sort column Descending order in Pivot based on selected cell in a column.
For some reason it doesn't sort all the numbers correctly in order, however it does sort in order if i do a manual sort.
Could someone look at this or suggest another code i could use?
I currently have the below code to sort column Descending order in Pivot based on selected cell in a column.
For some reason it doesn't sort all the numbers correctly in order, however it does sort in order if i do a manual sort.
Could someone look at this or suggest another code i could use?
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
VBA Code: