Pivot Table Issues

How to Sort an Excel PivotTable by Multiple Fields Using VBA

Elise WilliamsElise Williams Nov 4, 2026 869 views

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.

How to Sort an Excel PivotTable by Multiple Fields 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.
Before you start

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.

Solution 1Recommended

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.

1
Start Recording the Macro

Navigate to the Developer tab on the ribbon and click 'Record Macro'. Give the macro a recognizable name, such as 'MultiLevelPivotSort', and click OK.

2
Apply the Primary Sort Manually

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.

3
Apply the Secondary 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'.

4
Stop Recording and Open the VBA Editor

Return to the Developer tab and click 'Stop Recording'. Then, press Alt + F11 to open the Visual Basic Editor.

5
Adapt the Generated VBA Code

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.

Record and Adapt a Macro for Multi-Level Sorting
Test on a Copy: Always test your adapted VBA code on a copy of your workbook. Recorded PivotTable sort scripts heavily depend on specific field names, which might trigger errors if your source data headers change.
Efficient Spreadsheet Management

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. 1. Open your Macro-Enabled Workbook: Launch WPS Spreadsheet and open your .xlsm file containing the PivotTable.
  2. 2. Access the VBA Editor: Navigate to the Developer tab and click on 'VBA Editor' to access the macro environment.
  3. 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.
Full compatibility with Microsoft Excel .xlsx and .xlsm file formatsBuilt-in VBA macro editor for seamless automation in premium versionsIntuitive interface for manual multi-level PivotTable sortingLightweight application that runs smoothly even with large datasets
microsoft office alternative - wps office

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.