Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Looking Backward through a Data Table.
by Allen Wyatt
(last updated January 3, 2020)
Kirk has a large data table in Excel. Each row has a vehicle number, date (the table is sorted by this column), beginning mileage, and ending mileage. He would like to search backwards through the data table to find the ending mileage for the same vehicle number to use as the beginning mileage in the current row—similar to VLOOKUP but looking bottom to top rather that top to bottom.
There are several ways you can approach this with a formula. Assume, for this example, that the vehicle number is in column A, the date in column B, the starting mileage in column C, and the ending mileage in column D. What you need is a formula you can put in column C that looks up the most recent ending mileage for current vehicle. The following formula provides one approach; you should place it in cell C3:
You can copy the formula down the column as far as you need. If the vehicle number in column A has not appeared earlier in the data table, then the formula will return an error such as #VALUE! or #N/A. In that case, you can easily type over the formula with the starting mileage that you want to use for the vehicle.
Here's another formulaic approach, but this one should be entered as an array formula (by pressing Ctrl+Shift+Enter):
Again, place the formula in cell C3 and copy it down as far as needed. This one doesn't return an error value if the vehicle hasn't appeared earlier in the data table; it returns a value of 0. You can then type over the formula with the real starting mileage for that vehicle. The following array formula could also be used:
The difference with this array formula is that if the vehicle hasn't appeared earlier in the data table, it returns a #REF! error.
Here are two array formulas that are even shorter that you can use in C3 (and, again, copy down as needed):
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11744) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Looking Backward through a Data Table.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
The VLOOKUP function is very powerful, but it will only return values that meet a very limited set of criteria. If you ...Discover More
VLOOKUP is an oft-used worksheet function to lookup values in a data table. If the function cannot return a value, it ...Discover More
Want to be able to take information that is in one cell and match it to data that is contained in a table within a ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.