Wednesday, September 17, 2014

How to Sort Pivot Table Row Labels, Column Field Labels and Data Values with Excel VBA Macro

Macro To Sort Pivot Table Row Labels, Column Field Labels and Data Values
Sub Sort_Pivot_Row_Column_Data()    
Range("G3").Select    
'To Sort Descending the Column Labels
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Sales_Period").AutoSort _
        xlDescending, "Sales_Period"
        
'To Sort Descending the Row Labels
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Prod_Id").AutoSort _
        xlDescending, "Prod_Id"
        
'To Sort Descending the Data Values based on particular Column Label(3)
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Prod_Id").AutoSort _
        xlDescending, "Sum of Sales", ActiveSheet.PivotTables("PivotTable1"). _
        PivotColumnAxis.PivotLines(3), 1     
End Sub

Example :
Sample Pivot Data Table

To Sort Descending the Row Labels
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Prod_Id").AutoSort _
        xlDescending, "Prod_Id"

Output :

To Sort Descending the Column Labels

    ActiveSheet.PivotTables("PivotTable1").PivotFields("Sales_Period").AutoSort _
        xlDescending, "Sales_Period"

Output :

To Sort Descending Data Values based on particular Column Label(3) ="Q3-2014"

    ActiveSheet.PivotTables("PivotTable1").PivotFields("Prod_Id").AutoSort _
        xlDescending, "Sum of Sales", ActiveSheet.PivotTables("PivotTable1"). _
        PivotColumnAxis.PivotLines(3), 1

Output :

--------------------------------------------------------------------------------------------------------
Thanks, TAMATAM ; Business Intelligence & Analytics Professional
--------------------------------------------------------------------------------------------------------

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.