How to Sort an Excel PivotTable by a Selected Date Dynamically
Question details
The user needs to dynamically sort a PivotTable column from largest to smallest based on a date chosen from a data-validation drop-down list using VBA.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Automating the sorting process of a PivotTable by a specific date column using a macro triggered by a cell value change.
- Observed behavior
- The user requires the VBA SortFields method to accept a dynamic date column instead of a fixed number, while ensuring that sheet names, PivotTable names, and date formats are correct to avoid macro runtime errors.
Before running any VBA macro, verify that your source dates are formatted as real Excel date values rather than text strings. Text-based dates will cause the macro to fail or sort alphabetically instead of chronologically.
Use VBA to Dynamically Sort by Selected Date
Modify your VBA macro to identify the matching PivotTable column based on the selected date cell, bypassing the need for a fixed column number.
To make your PivotTable sort dynamically, your VBA code must read the target date from your data validation cell, locate the matching column inside the PivotTable, and apply the SortFields method to that specific column.
Check that the cell reference (e.g., A3) for your date drop-down is accurate and confirm that the worksheet name in your code exactly matches your workbook tab (e.g., 'INCREASE').
Write your VBA code to loop through the PivotTable's ColumnFields or DataFields to find the name that corresponds exactly with your selected date value.
Instead of using a hardcoded column index, pass the dynamically identified column variable to the PivotTable.SortFields method.
Configure the sorting parameters within the VBA method to set the Order to xlDescending (largest to smallest) to complete the macro setup.

Use Power Pivot and Timelines (No-Code Alternative)
If you are using a modern version of Excel, you can use a Timeline filter to handle dates dynamically without writing complex VBA code.
Dynamically Sort PivotTables Using WPS Spreadsheet
WPS Spreadsheet offers robust PivotTable features, complete VBA/macro support, and intuitive data validation tools, making it easy to create dynamic dashboards and automate sorting based on selected dates.
- 1. Open your Workbook: Open your existing .xlsm or .xlsx workbook in WPS Spreadsheet.
- 2. Enable Macros: If prompted by the security warning, click 'Enable Macros' to allow your VBA sorting script to run.
- 3. Verify Date Formats: Select your source date columns, right-click, choose 'Format Cells', and ensure a valid Date format is applied.
- 4. Use Data Validation: Use your existing data validation drop-down menu in the worksheet to select your target date.
- 5. Execute Sort: Trigger your macro (either automatically via a Worksheet_Change event or manually) and watch the WPS PivotTable dynamically sort the corresponding column.

Frequently Asked Questions
Why does my VBA macro fail at the SortFields section?
This usually happens if the worksheet name or PivotTable name in your VBA code does not precisely match the actual names in your workbook. Double-check references like Worksheets("INCREASE").PivotTables("PivotTable1") for typos or extra spaces.
How can I check if my dates are real Excel dates or stored as text?
Select your date cells, right-click, and choose 'Format Cells'. Apply a drastically different date format (e.g., changing MM/DD/YYYY to a Long Date like 'Monday, January 1'). If the appearance doesn't update, the data is being treated as text.
Can I sort a PivotTable by date dynamically without using VBA?
Yes. If you prefer not to use code, you can insert a 'Timeline' from the PivotTable Analyze ribbon in newer Excel versions. This provides an interactive visual slider to filter and sort your dates dynamically.
What is a data-validation date selection?
It is a drop-down list created using the Data Validation feature (found in the Data tab) that restricts user input to a predefined list of valid dates. In this automated setup, it serves as the user interface for telling the macro which date column to sort.




