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.

Looking Backward through a Data Table

by Allen Wyatt
(last updated January 3, 2020)

3

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 (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.

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

Using Custom Number Formats

Most formatting needs are met by using the predefined formatting options in Excel. The program also allows you to move ...

Discover More

Changing Horizontal Orientation

One of the international features of Excel is the ability to switch the orientation of how information is presented. This ...

Discover More

Passing a Data File Name via Command Line to a Macro

Using the command line to pass parameters to a program is a common occurrence. Using the command line to pass parameters ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (menu)

Returning Blanks or Asterisks from a Lookup

Want to return more than a value when doing a lookup? Here's one way to do it by adding an IF clause to your formula.

Discover More

Returning a Weight and a Date

If you have two columns containing dates and weights from those dates, you may want to pick a date associated with a ...

Discover More

Looking Up Names when Key Values are Identical

Need to look up some values based upon some key items that may be identical to each other? Depending on the ...

Discover More
Subscribe

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

View most recent newsletter.

Comments

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}] (all 7 characters, in the sequence shown) 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 five minus 3?

2021-12-03 06:26:49

Yogananda

(see Figure 1 below)

How to use vlookup for filling the columns g,h,i for each vehicle number

Figure 1. 


2021-10-10 04:42:12

Willy Vanhaelen

Another very simple method to obtain the desired result, without formulas nor copying down, is to filter the table for the vehicle number. The last row shows the wanted mileage.


2021-10-09 10:20:39

Roy

Of course, TODAY (2021) (and in 2020, at first only if you were a beta (yeah... or alpha...) tester, er, I mean "Office Insider", of course) but after a while in 2020) the best way to do this would be to use XLOOKUP() for most searches like this. It lets you search end toward beginning so... kind of a slam dunk unless it somehow can't fit the precise situation (which does happen).

But... if you want, or need, the old school way, the LOOKUP() solution is pretty traditionally old school, but there's another old school way that works nicely and it has the advantage of its workings being more "intuitively obvious to the casual observer" (and I don't mean in the sarcastic way that was used as a joke in college around 1980) and so is easier to maintain.

It uses INDEX(). INDEX() can do many things, but one is that it can invert a range for you. The trick was slightly obtuse, but understandable, then, and even clearer now we also have SEQUENCE(). That trick is to specify the rows in reverse order.

You might think you can't, because Excel "regularizes" any range addressing you use: give it D2:A7 and it will not only make the column references A:D rather than D:A, it will also use the row references with the "proper" column order to specify the same rectangle, which looks a lot like it is switching the example's rows around as well, though you can see it isn't really. So it regularizes D2:A7 to be A2:D7. That means the usual old school trick to get a number sequence, "ROW(1:5)" to get {1,2,3,4,5} won't directly get a reverse sequence like {5,4,3,2,1} just by using "ROW(5:1)" because Excel will "fix" that to be "ROW(1:5) and give you the rising sequence instead. (That's why SEQUENCE() is so nice: it does what it's told, not what it hears. So "SEQUENCE(5,1,5,-1) gives you the falling sequence {5,4,3,2,1}, as desired.)

BUT... if you know the number of values you need (the number of "rows" you want to use in "1:x"), you can take the next higher integer and SUBTRACT the usual ("ROW(1:5)") trick. So "6-ROW(1:5)" gives you {5,4,3,2,1}. And you do usually know the number of rows just from looking at the range address you entered (or using ROWS() on the Named Range, etc.). As a minimum, you can use something complicated looking like "ROWS(A1:B5)+1-ROW(INDIRECT("1:"&ROWS(A1:B5)))" where the first two terms figure the number to subtract from and the last term builds the "ROW(1:x)" part of it.

So, say your range is E1:F5 and you'd like to search it in reverse using VLOOKUP(). For the search range parameter, use the following INDEX() function:

INDEX(E1:F5,ROWS(E1:F5)+1-ROW(INDIRECT("1:"&ROWS(E1:F5))),{1,2})

it rearranges the table from bottom to top, neatly inverting it. The last parameter in it ("{1,2}") specifies the columns and keeps them in the same order. That's what's desired in this problem. But if you want a fuller inversion, you could reverse that as well ("{2,1}"). Lots of possibilities there.

Why bother if you have XLOOKUP() handy, eh? But if you don't, or it won't fit the situation, just do the above. If you have SEQUENCE() available, use it instead of all the ROW(), ROWS(), and INDIRECT() portions.

One situation that XLOOKUP() doesn't fit so wonderfully, because it WON'T return a 2-D result (actually, to be precise, I don't think it can FORM a 2-D result, hence also cannot return one), is, well... when you need a 2-D result...

If using FILTER() to form the table that will be inverted, then unlike most times when using FILTER()'s results inside a formula, instead of basing the rows for the sequence on the rows in the range FILTER() is working upon, one needs to base it upon the number of rows FILTER() is feeding forward into the rest of the formula. Otherwise one gets extra lines of return which contain errors and that might affect one's formula's approach as it might not handle those errors in a desirable way.


This Site

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.

Newest Tips
Subscribe

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

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.