UiPath doesn’t have a direct activity to sort within a pivot table in Excel. However, there are two workarounds you can consider to achieve your desired sorting but you can check in UiPath Marketplace:
Got code from internet but @marco.roensch you can try this
1. Leverage VBA Code (Invoke Code Activity):
This approach utilizes VBA code to interact with the Excel object model and sort the pivot table. Here’s a basic example:
Sub SortPivotTable()
ActiveSheet.PivotTables(1).PivotFields("**Field to Sort**") _
.AutoSort xlDescending, "**Value Field Name**"
End Sub
Explanation:
Replace “Field to Sort” with the actual name of the column you want to sort (e.g., “Product”).
Replace “Value Field Name” with the name of the value field you want to sort by (e.g., “Sales”).
Use the “Invoke Code” activity in UiPath and paste this VBA code into it.
2. Sort After Exporting Data (Sort Data Table Activity):
Export Data: Use the “Excel Application Scope” and “Read Range” activities to extract the data from the pivot table into a UiPath DataTable.
Sort Data: Utilize the “Sort Data Table” activity. Specify the column to sort by (the second column) and the sort order (descending).
Write Back (Optional): If needed, use the “Excel Application Scope” and “Write Range” activities to write the sorted data back to a new location in your Excel sheet.
Choosing the Right Approach:
If you prefer a non-VBA solution and don’t mind an extra step of writing data to a separate table, use method 2.
If you’re comfortable with VBA and want a more streamlined approach, use method 1.
Remember to adjust the code snippets and activity properties based on your specific pivot table structure and desired outcome.