Wednesday, 17 September 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

No comments:

Post a Comment

Follow Me by Email

ExcelKingdom-Popular Posts

ExcelKingdom-Random Posts