Function Problems

Keep Excel References Correct When Sorting Source Data

Steve KSteve K Nov 18, 2026 868 views

Question details

Keep dependent values associated with the correct records when source data is sorted.

Keep Excel References Correct When Sorting Source Data
Product
Microsoft Excel
Device & OS
not provided
Scenario
A worksheet refers to records on another sheet, and the source records need to be sorted.
Observed behavior
Direct cell references can point to values belonging to different records after the source data is sorted.
Before you start

Check that each source record has a unique ID and that the dependent sheet contains the same IDs. When sorting, select the entire source table so each record's fields move together.

Solution 1Recommended

Look up each record by ID with INDEX and MATCH

Use an ID-based lookup instead of a fixed cell reference to retrieve the correct value after sorting.

In this example, the Results sheet has unique IDs in B4:B9 and the values to return in D4:D9. Cell B2 on the dependent sheet contains the ID to find.

1
Check the record IDs

On the Results sheet, confirm that B4:B9 contains unique IDs. On the dependent sheet, enter the matching ID in B2.

2
Enter the lookup formula

Select the dependent cell where you want the value and enter =INDEX(Results!$D$4:$D$9,MATCH($B2,Results!$B$4:$B$9,0)). Adjust the sheet name and ranges to match your data.

3
Copy the formula

Drag the formula cell's fill handle down to look up the IDs in the rows below. The source ranges stay fixed while the ID reference changes by row.

4
Sort and verify

On the Results sheet, select the entire source table, then use Data > Sort. Check that the dependent values still match their record IDs.

Look up each record by ID with INDEX and MATCH
Use unique, matching IDs: MATCH returns the first match. Duplicate IDs can therefore return the wrong record, while a missing or differently formatted ID can produce an #N/A error.
Sort-safe spreadsheet lookups

Keep sorted records linked in WPS Spreadsheets

Use INDEX and MATCH in WPS Spreadsheets to retrieve values by record ID rather than by a fixed source-cell position.

  1. 1. Open the workbook: Open WPS Spreadsheets and select Open to load your Excel workbook.
  2. 2. Check the IDs: On the source sheet, verify that each record has a unique ID. Enter the corresponding IDs on the dependent sheet.
  3. 3. Add the lookup: Select a dependent cell and enter =INDEX(Results!$D$4:$D$9,MATCH($B2,Results!$B$4:$B$9,0)), replacing the example ranges with your own.
  4. 4. Sort the source table: Select the complete source table and use Data > Sort. Verify that the returned values still correspond to the IDs.
  5. 5. Save the workbook: Use File > Save As and choose XLSX if you want to retain an Excel-compatible file.
Keep dependent values matched to record IDs after sortingUse familiar INDEX and MATCH formulasOpen and edit Microsoft Excel XLSX filesWork with spreadsheets using a familiar interface
microsoft office alternative - wps office

Frequently Asked Questions

Why can a direct cell reference show the wrong record after sorting?

A reference such as =Results!D4 points to a cell position, not a unique record ID. If sorting places a different record in that position, the dependent cell can show the wrong record's value.

Can I use XLOOKUP instead of INDEX and MATCH?

Yes, if your Excel version supports XLOOKUP. For the example ranges, enter =XLOOKUP(B2,Results!$B$4:$B$9,Results!$D$4:$D$9) in the dependent cell.

Why does my lookup return #N/A?

Check that the ID in the dependent sheet exists in the source ID column and has the same format. For example, a number stored as text may not match a numeric ID.

What happens if I sort only one column of the source data?

Sorting one column independently can separate IDs from their related values. Select the entire source table before choosing Data > Sort so each row stays together.