Sort Pivot Table based on selected Cell

NateD1

New Member
Joined
Apr 1, 2020
Messages
46
Office Version
  1. 365
Platform
  1. Windows
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

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
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
my other pivot which has multiple columns of data but comes from only one value in the field settings
Perhaps I just haven't had enough caffeine yet, but I don't understand what you mean by this. Can you elaborate, perhaps with a screenshot?
 
Upvote 0
Perhaps I just haven't had enough caffeine yet, but I don't understand what you mean by this. Can you elaborate, perhaps with a screenshot?
sorry for the confusion, i have just recorded the below macro that id actually like it to sort my pivot table by any column that I click on, and not just fixed to column (6) as shown in the code below. is it possible to amend the below to do this? thanks :)

VBA Code:
Sub SortPivot()

    ActiveSheet.PivotTables("PivotTable1").PivotFields("NAME").AutoSort _
        xlDescending, "Sum of Time", ActiveSheet.PivotTables("PivotTable1"). _
        PivotColumnAxis.PivotLines(6), 1
End Sub
 
Upvote 0
I think you want something like this:

VBA Code:
Sub Pivot_Sort_by_Item()
   Dim pt As PivotTable
   Dim pi As PivotItem
   
   On Error Resume Next
   Set pt = ActiveCell.PivotTable
   If pt Is Nothing Then Exit Sub
   Set pi = ActiveCell.PivotItem
   On Error GoTo 0
   If Not pi Is Nothing Then
      pt.PivotFields("NAME").AutoSort _
              xlDescending, "Sum of Value", pt.PivotColumnAxis.PivotLines(pi.Position), 1
   End If
End Sub
 
Upvote 0
thank you! the code works but for some reason when i select the column to sort.. it sorts the column to right of the one clicked and when i click the last column it errors the macro as there is no column to the right. below is a screenshot of my data fields. any idea why this would be happening?
 

Attachments

  • Screenshot1.png
    Screenshot1.png
    5.7 KB · Views: 2
Upvote 0
Do you have any values in the Reason code column that are not visible in the pivot table?
 
Upvote 0
its working now! :) thank you!
 
Last edited:
Upvote 0
Then I'm confused. It works fine here. Perhaps try this, which simply uses the relative position of the active cell in its parent field's label range:

VBA Code:
Sub Pivot_Sort_by_Item()
   Dim pt As PivotTable
   Dim pi As PivotItem
   
   On Error Resume Next
   Set pt = ActiveCell.PivotTable
   If pt Is Nothing Then Exit Sub
   Set pi = ActiveCell.PivotItem
   On Error GoTo 0
   If Not pi Is Nothing Then
      Dim idx As Long
      idx = ActiveCell.Column - pi.Parent.LabelRange.Column + 1
      pt.PivotFields("NAME").AutoSort _
              xlDescending, "Sum of Value", pt.PivotColumnAxis.PivotLines(idx), 1
   End If
End Sub
 
Upvote 0
Solution
apologies, was my error- had to change the word value to Time, thanks for you help on this one
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,970
Members
449,095
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top