 Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Looking Backward through a Data Table.

# Looking Backward through a Data Table by Allen Wyatt
(last updated November 14, 2015)

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:

```=LOOKUP(2,1/FIND(A3,A\$2:A2,1),D\$2:D2)
```

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):

```=IF(A3="","",MAX(IF((\$A\$2:A2=A3)*(\$D\$2:D2),\$D\$2:D2)))
```

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:

```=IF(A3="","",INDIRECT("D"&LARGE((\$A\$2:A3=A3)*ROW(\$2:3),2)))
```

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):

```=MAX((D\$2:D2)*(--(A\$2:A2=A3)))
=MAX(IF(A\$2:A2=A3,D\$2:D2))
```

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11745) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Looking Backward through a Data Table.

##### Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

##### MORE FROM ALLEN

Changing How Footnote References Appear

Footnote references normally appear as superscripted digits, both in the main body of your document and in the footnotes ...

Discover More

Columns in a Text Box

Want to divide a text box into columns? Word doesn't allow you to do this, but there are ways to work around the limitation.

Discover More

Putting Template Macros in a Document

You can easily store your macros in a template. If you create a document based on the template and then the document is ...

Discover More Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

##### More ExcelTips (ribbon)

Returning Zero When a Referenced Cell is Blank

Reference a cell in a macro, and if that cell is blank Excel normally equates that to a zero value. What if you don't ...

Discover More

Generating Random Strings of Characters

If you need to generate a random sequence of characters, of a fixed length, then you'll appreciate the discussion in this ...

Discover More

Extracting a State and a ZIP Code

Excel is often used to process or edit data in some way. For example, you may have a bunch of addresses from which you ...

Discover More
##### Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 4 - 0?

2018-02-28 12:08:38

Peter Atherton

Anthony

To subtract Column G from Column F use
=LOOKUP(2,1 / (ISNUMBER(A6:G6)),A6:G6)-LOOKUP(2,1 / (ISNUMBER(A6:F6)),A6:F6)

To subtract g6 from g7 use
=LOOKUP(2,1 / (ISNUMBER(A7:G7)),A7:G7)-LOOKUP(2,1 / (ISNUMBER(A6:G6)),A6:G6)

2018-02-27 09:40:30

Anthony Dauer

Hello,

I've used the formula =LOOKUP(2,1 / (ISNUMBER(A1:G1)),A1:G1) to find the data in the last cell with data in it in a row and now want to subtract that cell from the cell previous to it. The goal being to determine how long a period of time has occurred between the two last date entries in a row. Would this formula combined be able to do that?

Sincerely,

Anthony

##### This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.