How to Sort an Excel PivotTable by Multiple Fields Using VBA
Question details
The user needs to automate the sorting of an Excel PivotTable by multiple fields, such as applying a primary alphabetical sort and a secondary numerical sort, using VBA.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating a multi-level sort in a PivotTable (e.g., sorting Country in ascending order and Sum of Total Qty in descending order).
- Observed behavior
- While single-field sorting is simple, applying a secondary sort programmatically requires precise VBA properties, PivotField names, and syntax.
Ensure the Developer tab is enabled in your ribbon and that your workbook is saved as a Macro-Enabled Workbook (.xlsm) to retain your VBA scripts.
Record and Adapt a Macro for Multi-Level Sorting
The most reliable way to get the exact VBA syntax for multi-level PivotTable sorting is to record a macro while performing the action manually, then adapt the generated code.
Because PivotTable properties can be complex and vary slightly depending on your Excel version, recording a macro captures the exact field captions, data options, and sort orders required for your specific data set.
Navigate to the Developer tab on the ribbon and click 'Record Macro'. Give the macro a recognizable name, such as 'MultiLevelPivotSort', and click OK.
Go to your PivotTable, click the drop-down arrow on the primary row field (e.g., Country), and select 'Sort A to Z' to apply the ascending sort.
Right-click a value in the data column you want to sort secondarily (e.g., Sum of Total Qty), hover over 'Sort', and select 'Sort Largest to Smallest'.
Return to the Developer tab and click 'Stop Recording'. Then, press Alt + F11 to open the Visual Basic Editor.
Locate your recorded macro in the 'Modules' folder. Review the code to ensure the PivotTable names and PivotField references match your requirements. Clean up the code to make it dynamic if needed.

Automate and Manage PivotTables with WPS Office
WPS Spreadsheet provides a highly compatible environment for creating, managing, and sorting PivotTables. It supports VBA macros in its advanced editions, allowing you to easily automate complex multi-level sorting tasks just as you would in Microsoft Excel.
- 1. Open your Macro-Enabled Workbook: Launch WPS Spreadsheet and open your .xlsm file containing the PivotTable.
- 2. Access the VBA Editor: Navigate to the Developer tab and click on 'VBA Editor' to access the macro environment.
- 3. Paste and Run Your Code: Insert your customized PivotTable sorting VBA script into a module and run the macro to instantly apply the multi-level sort to your data.

Frequently Asked Questions
Can I manually type the VBA sort code without recording a macro?
Yes, you can manually use the PivotFields("FieldName").AutoSort method, specifying the sort order (e.g., xlAscending or xlDescending) and the sorting field key. However, recording a macro first is highly recommended to accurately capture the specific syntax and field captions of your PivotTable.
Why does my VBA code return an error when sorting the PivotTable values?
This usually happens if the value field's caption has changed, or if the PivotTable name referenced in the code differs from the actual PivotTable. Ensure that the field references exactly match the headers in your current PivotTable layout.
Can WPS Spreadsheet run my existing Excel VBA macros?
Yes, WPS Office supports VBA macros in its advanced and professional versions. You can run, edit, and record macros in WPS Spreadsheet with high compatibility to Excel's VBA syntax.




