Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. 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: Retrieving the Last Value in a Column.
by Allen Wyatt
(last updated December 9, 2016)
You may wonder if there is a way to return the last (not largest) value in a column. For instance, if there are values in A1 through A5, then you may want the value in A5 returned. Later, if values were added in A6 through A8, then the value in A8 should be returned.
There are a couple of ways that a solution can be approached. The first is to use a formula such as the following:
This formula returns the last numeric value in a column, providing that the values begin at (in this case) A1. This approach only works if all the values in the column are numeric. If the values are non-numeric, or if there are blank cells intermixed with the values, then a different approach is necessary. One way is to copy the following formula into column B, just to the right of the cells that may contain values:
In this case, the formula returns the row number of any cell in A which contains a numeric value greater than zero. The following formula can then be used to retrieve the last value in column A:
This formula works because it returns the largest row number from column B, and then uses that as an index to return the corresponding value from column A.
If you don't want to use a helper column (as is done here in column B), you can use the following formula if there is a mix of numeric and non-numeric values in column A:
This formula may need a bit of explaining. The ISBLANK(A:A) portion returns an array that lists a FALSE value in each spot corresponding to a cell in column A that contains a value and a TRUE value in each spot corresponding to a cell in column A that is blank. These TRUE/FALSE values are then subtracted from 1 so that it changes to 0/1 values instead of TRUE/FALSE values.
This array is then "inverted" by dividing 1 by the 0/1 values, resulting in an array that contains a 1 wherever there's a value in column A and a #DIV/0! error wherever column A has a blank cell. Finally, the LOOKUP function looks for the value 2 in the array. It won't find it (there are only 1s and errors in the array), so it returns the last "1" in the list and thereby fetches the last non-blank value from the column.
As you can tell, returning the last value in a column can get a bit tricky at times. A clean approach is to simply develop your own VBA function that returns the desired value. In this case you can program the function to return any value—not just numeric values. A fine example of such a function is available at John Walkenbach's Web site. Check out the following:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9008) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Retrieving the Last Value in a Column.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
AutoFill is a great feature. It can detect patterns and adjust cell contents as you drag a selection on-screen. It ...Discover More
If you have a full path designation for the location of a file on your hard drive, you may want a way for Excel to pull ...Discover More
The primary way you signify that you are entering a formula is to start a cell entry with an equal sign. The equal sign ...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.