Hello Fizzy,
You can sort the double clicked data in Pivot table. Try below code.
Regards,
Thangavel D
Option Explicit
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim i As Integer
Dim s() As String
' When a new sheet is activated, we need to check if our global variable is set to true, if yes a pivot table was double clicked...
If pvtdoubleclicked Then
'.. and now let's apply the sort key to the data set returned
For i = UBound(keys_sort) To LBound(keys_sort) Step -1
s = Split(keys_sort(i), "*")
ActiveCell.CurrentRegion.Sort key1:=ActiveSheet.Cells(1, Val(s(0))), Order1:=Val(s(1)), Header:=xlYes
Next
pvtdoubleclicked = False
End If
End Sub
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Dim rng As Range
Dim key_sort As SortField
Dim i As Integer
' If the cell double-clicked is not a pivot table a run time error will occur, so
' let's avoid this with On Error statement
On Error GoTo ExitPoint
' Set rng to pivot table source data
Set rng = Application.Range(Application.ConvertFormula(Target.PivotTable.SourceData, xlR1C1, xlA1))
' Set pvtdoubleclicked to true, so the spreadsheet project will know that a pivot table was double-clicked
' when a new worksheet is created
pvtdoubleclicked = True
' Read and store the sort keys used by pivot table data source
ReDim keys_sort(rng.Worksheet.Sort.SortFields.Count - 1)
i = 0
For Each key_sort In rng.Worksheet.Sort.SortFields
keys_sort(i) = key_sort.key.Column - rng.Column + 1 & "*" & key_sort.Order
i = i + 1
Next
Exit Sub
ExitPoint:
pvtdoubleclicked = False
End Sub
Bookmarks