Automate Excel Station Distance Calculations with XMATCH
Question details
The user needs to calculate the exact number of stations between a selected departure and arrival station based on a master list located on another sheet, without manually updating cell references.
- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating distance calculations between two points selected from a predefined list of stations.
- Observed behavior
- The goal is to automatically output the absolute difference in station positions whenever a valid departure and arrival station are selected.
Ensure your station list is formatted as an Excel Table and that you are using a version of Excel or WPS Spreadsheet that supports the XMATCH function.
Use XMATCH and ABS Functions to Calculate Distance
Combine the XMATCH function with ABS to dynamically find the exact number of stops between two stations.
The XMATCH function searches for a specified item in an array and returns its relative position. By finding the numerical position of both the departure and arrival stations in the master list, you can subtract one from the other. The ABS function then ensures the result is always a positive number, regardless of travel direction.
Format your master list of stations as an Excel Table. For this example, assume the table is named 'Ligne2' and the column containing the station names is called 'Stations'.
In your main worksheet where calculations occur, create two columns named 'Departure' and 'Arrival' for users to select their stations.
In the destination column for the distance, enter the following formula: =IF(OR([@[Departure]]="",[@[Arrival]]=""),"",ABS(XMATCH([@[Departure]],Ligne2[Stations])-XMATCH([@[Arrival]],Ligne2[Stations])))
If your table has a different name, such as 'Table10', replace 'Ligne2[Stations]' with your actual table and column references.
Easily Calculate Data Distances with WPS Spreadsheet
WPS Spreadsheet fully supports advanced dynamic array functions like XMATCH, making complex distance and position calculations a breeze. Use WPS Office to effortlessly manage your tables and formulas.
- 1. Open WPS Spreadsheet: Launch WPS Office and open the workbook containing your station data.
- 2. Format Data as a Table: Highlight your master station list and press Ctrl+T to quickly format it as an easily referenceable table.
- 3. Input the Formula: Select the target cell for the calculation and enter the XMATCH and ABS formula.
- 4. Calculate Automatically: Press Enter to instantly calculate the absolute distance between your stations. Drag down to apply to multiple rows.

Frequently Asked Questions
What is the difference between MATCH and XMATCH in Excel?
XMATCH is a newer, more robust function that defaults to an exact match, unlike the older MATCH function which requires a third argument for exact matching. XMATCH also supports searching from last to first natively.
Why use the ABS function in this calculation?
The ABS (Absolute) function converts any negative result into a positive number. This ensures that the distance between stations is always a positive integer, regardless of whether you are traveling up or down the station list.
Can I use XMATCH across different worksheets?
Yes. As long as you reference the correct table or range name, XMATCH can look up data located on any worksheet within the same workbook seamlessly.
What happens if I get a #NAME? error when using this formula?
A #NAME? error typically means you are using an older version of Excel that does not support the XMATCH function, or there is a typo in your table or column references.




