Written by Allen Wyatt (last updated July 17, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
Marc has a worksheet in which he keeps produce prices for a community group. Each row represents a produce item (apples, carrots, lettuce, etc.) and each column represents a purchase date. Thus, the intersection of each row and column represents the price at which a produce item was purchased on a particular date. Not all items are purchased on any given date. Marc needs a formula that will, for a particular produce item, return the rightmost value (price) for that item.
An easy way to handle this is to use the LOOKUP function. Let's say that your dates columns are B:N. If you want to figure out the latest price (rightmost value) in row 2, you could use the following:
This works because LOOKUP will work with only two parameters. The first is the value you want to look up and the second is the array (range of cells) in which to look for the value. If the value in the first parameter is found, then the value is returned. In other words, if you had the value 999 in one of the cells B2:N2, then 999 is returned.
However, the trick here is to specify a value in the first parameter that is much larger than the largest anticipated purchase price. If you anticipated that you might purchase something for $999 or more, then you could simply increase the value in the first parameter:
When LOOKUP cannot find the desired value, it returns the last value in the array—the last price shown in the range of cells.
The biggest drawback to this approach is that you'll need to remember to update the range of cells when you add more columns of data. You could get around this by simply making the range much larger than what you need, as shown here:
Of course, if you ever go past column AZ with your data, you still run into the same need to update the range specification. If you don't want to worry about needing to do this, you could combine use of the INDEX and MATCH functions, in this manner:
In this formula, the MATCH function tries to match, within row 2, a value that equals 9^9. If that cannot be located, then MATCH returns the column number of the last value in the row. This is then used as an offset for the INDEX function, which returns the value at the cell offset in row 2. This is, of course, the very rightmost value in the row.
For additional possible formula ideas, see this tip:
The biggest difference between the ideas in that tip and this one is that it needs to account for the possibility of zero-value formula results being in the rightmost cells of a row, whereas Marc's situation doesn't. (This means that Marc can use simpler formulas than are included in the other tip.)
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13884) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.
Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!
Excel allows you to easily combine text together. Interestingly, it provides two ways you can perform such combinations. ...Discover More
If you need to switch between viewing formulas and viewing the results of those formulas, you'll love the keyboard ...Discover More
You can use the Alt+Enter keyboard shortcut while entering information in order to force your data onto multiple lines in ...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 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.