Safely Sort an Excel Table with Cross-Row References
Question details
The user needs to sort an Excel table while preserving the intended relationships between rows used by formulas.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A table contains formulas that depend on values in other rows and needs to be sorted.
- Observed behavior
- Relative cross-row references can produce different results after the table is sorted.
Identify which formulas refer to other rows and determine which records they should reference. Give each referenced record a unique, stable ID and record the intended related ID for each formula row before sorting.
Use stable IDs and lookup formulas before sorting
Use this approach when formulas must continue to find the same records after their rows move.
A lookup based on a unique ID identifies a record rather than relying on its position. The example assumes a table named Table1 with ID, RelatedID, and Amount columns.
In Table1, enter a unique value in each ID cell. In RelatedID, enter the ID of the record whose Amount the current row should use.
In the result column, enter =INDEX(Table1[Amount],MATCH([@[RelatedID]],Table1[ID],0)). Apply the formula to the column if Excel does not fill it automatically.
Open the filter arrow in the header of the column you want to sort, then choose Sort Smallest to Largest or Sort Largest to Smallest.
Check several result cells against their RelatedID values. If a cell shows #N/A, confirm that its RelatedID exists in the ID column.

Sort cross-row data with lookup formulas in WPS Spreadsheet
Use unique IDs and an INDEX/MATCH formula in WPS Spreadsheet to look up related records, then sort the complete data range.
- 1. Open the workbook: Open the Excel workbook in WPS Spreadsheet and identify the rows that formulas need to reference.
- 2. Add relationship IDs: Place unique record IDs in column A, amounts in column C, and the intended related record ID for each row in column D.
- 3. Enter a lookup formula: In E2, enter =INDEX($C$2:$C$100,MATCH(D2,$A$2:$A$100,0)). Adjust the range to cover all data rows, then fill the formula down column E.
- 4. Sort and check: Select the entire data range, including headers, and choose Data > Sort. Sort by the desired column, then check several results against their related IDs.

Frequently Asked Questions
Will absolute references keep a formula linked to the same record after sorting?
Not necessarily. A reference such as $A$2 fixes a cell position, while a record may move to another row. Use a unique ID and a lookup formula when the formula must follow a particular record.
Should I switch Excel to manual calculation before sorting?
Manual calculation can postpone recalculation, but it does not prevent reference changes or preserve relationships between records. Use Formulas > Calculation Options to check the mode, and recalculate after sorting if manual mode is enabled.
Why does my lookup formula show #N/A after sorting?
Check that the RelatedID exists in the ID column and that the values match. Also make sure the lookup range covers every data row.
Can I sort just one column of the data?
Sort the entire table or data range so each record's values stay together. In an Excel table, use the sort option in the desired column's header; for a regular range, select all related columns before choosing Data > Sort.




