How to Create Excel Formulas That Keep Working After Sorting Data
Question details
The user needs an Excel formula that calculates data correctly even after the dataset is sorted, avoiding calculation errors caused by shifting row positions.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Calculating aggregated amounts for specific transaction types (e.g., Visa) in a dataset that requires frequent sorting.
- Observed behavior
- Standard formulas that rely on relative or fixed cell references return incorrect results or break entirely when the underlying data is sorted.
Ensure your dataset has clear, unique column headers and consider converting your data range into an official Table layout before applying formulas, as this natively helps maintain data integrity during sorting.
Use Criteria-Based Functions Like AVERAGEIFS
Replace relative row references with criteria-based functions that dynamically search the entire column for specific identifiers, regardless of the row's sorted position.
Formulas based on fixed cell positions easily break when a sort action changes the row order. Criteria-based formulas solve this by identifying target data through text descriptions rather than row numbers.
Determine which column holds the text identifiers (e.g., Column B for transaction descriptions) and which holds the numerical values (e.g., Column C for amounts).
Click on a cell completely outside of the data range you intend to sort. This prevents the formula cell itself from moving during the sort action.
Type the formula =ROUND(AVERAGEIFS($C:$C,$B:$B,"Visa"),2) and press the Enter key. The absolute column references ($C:$C) ensure the formula checks the entire column constantly.

Combine SUMIFS and COUNTIF for Custom Calculations
An alternative approach is to manually calculate the average by dividing the conditional sum by the conditional count, which also relies on criteria instead of row placement.
Easily Manage and Sort Data Formulas with WPS Spreadsheet
WPS Office offers a powerful, free Spreadsheet application that fully supports advanced criteria-based functions like AVERAGEIFS and structured table references to keep your data accurate while sorting.
- 1. Open your dataset in WPS Spreadsheet: Launch WPS Office and open your workbook containing the transaction data.
- 2. Format data as a Table: Highlight your data range and press Ctrl+L to convert it into a Smart Table.
- 3. Input a criteria-based formula: In a cell outside the table area, type =AVERAGEIFS($C:$C,$B:$B,"Visa") to calculate the average for specific transactions.
- 4. Sort without errors: Use the drop-down arrows in your table headers to sort the data freely. The formula will automatically adapt and remain accurate.

Frequently Asked Questions
Why do my formulas change or return #REF! when I sort data in Excel?
When you sort data, cell contents move to different rows. If your formulas use standard relative references (like A2 or B5) pointing inside the sorted range, Excel adjusts or loses track of those references, which disrupts the calculation.
What is the benefit of using absolute references ($) when sorting?
Absolute references (like $C:$C for an entire column) lock the formula to a specific structural range. When combined with criteria-based functions, they ensure the formula always evaluates the correct data area regardless of how the rows shift during a sort.
How do structured table references help with sorting data?
By converting your data into an official Table (Insert > Table), you can use structured references like Table1[Amount]. These references are bound to the data itself rather than grid coordinates, meaning they automatically adapt to sorting, adding, or deleting rows without breaking your formulas.




