Grabbing the Second-to-Last Value in a Column

by Allen Wyatt
(last updated February 4, 2017)

8

Cedric has a need to determine the value of the second-to-last cell in column to which he routinely adds values. He has seen formulas for grabbing the last value, but not the second-to-last.

There are many formulatic ways you could approach this need. In many respects, the method you choose will depend on the characteristics of the data in the column. Let's assume that your data is in column A, that it is numeric, and that there are no blanks in any of the cells in the column. In that case, you could use the following formula:

=INDEX(A:A,COUNT(A:A)-1)

Note that this formula will not return the correct result if there are any text values in column A—even if that text value is in a heading. If you want to compensate for the heading, you could modify the formula in this manner. (Note that it only checks the range of A2:A1000. If your data extends beyond row 1,000, then you'll need to modify the range specified.)

=INDEX(A2:A1000,COUNT(A2:A1000)-1)

If your numeric data may contain empty cells, then any of the following formulas will work just fine:

=OFFSET(INDIRECT("A"&MATCH(9^9,A:A)),-1,0)
=INDEX(A:A,MATCH(9.9E+23,A:A,1)-1,1)
=INDEX(A:A,(MATCH(LOOKUP(10000,A:A),A:A)-1),1)
=INDEX(A:A,AGGREGATE(14,6,ROW(A:A)*A:A/A:A,2))

You should recognize that if the second-to-last cell in the column is empty, then these formulas will return 0. (Well, except for the formula that uses the AGGREGATE function. It returns the value in the first non-empty cell before the second-to-last cell.)

If your data in column A can contain either numbers or text (but still no empty cells), then you need a different formula. Either of these will do:

=INDEX(A:A,COUNTA(A:A)-1,1)
=INDIRECT("A"&COUNTA(A:A)-1)

If there may be empty cells in your data, then you could use either of the following:

=INDEX(A:A,MATCH(LOOKUP(2,1/(A:A<>""),A:A),A:A,0)-1)
=INDEX(A:A,LOOKUP(2,1/(A:A<>""),ROW(A:A))-1)

Again, if the second-to-last cell is empty, then the formula returns 0.

Finally, you could also use the following type of formula, provided that your cells in the column use a named range (in this case MyData). Remember, though, that as you add more information to the column, you'll need to make sure that the named range includes the added data.

=INDEX(MyData,ROWS(MyData)-1)

You should note that virtually all of the formulas mentioned in this tip use a -1 in them. This indicates you don't want the last cell in the range, but the second-to-last ("one up" from the bottom of the range). If you want a different offset from the bottom of the range, you can change this portion of each formula. For instance, if you wanted 2 cells up from the bottom of the range, you would change all instances of -1 to -2.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (1523) applies to Microsoft Excel 2007, 2010, 2013, and 2016.

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 AutoText and AutoCorrect Effectively

AutoText and AutoCorrect are closely related tools that can help you improve the productivity of your typing. This tip ...

Discover More

Changing Cell Alignment

Individual cells in a table can be aligned any way you desire. As pointed out here, just select the cell and apply the ...

Discover More

Where Is that Text?

Looking for a formula that can return the address of a cell containing a text string? Look no further; the solution is in ...

Discover More

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!

More ExcelTips (ribbon)

Adding a Missing Closing Bracket

When working with large amounts of data, it is a good idea to make sure that the data all consistently follows a pattern. ...

Discover More

Removing Dashes from ISBN Numbers

ISBN numbers are used to denote a unique identifier for a published book. If you remove the dashes included in an ISBN, you ...

Discover More

Number of Terms in a Formula

Formulas are made up of operands that separate a series of terms acted upon by the operands. You may want to know, for some ...

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}] 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 two more than 4?

2017-08-25 16:28:13

MIchael Armstrong

Could you transpose the column and take the 2ndrow?


2017-08-24 12:30:03

Dennis Costello

Micky's formula is very intriguing ... however, it relies on unpublished behavior. The description of the LOOKUP function says "The values in Lookup_vector must be placed in ascending order ... otherwise, LOOKUP might not return the correct value." Each element of Lookup_vector contains either 1 or the error $DIV/0!. For no reason at all other than a quirk of the function's implementation, it stops looking for a 2 (which is of course nowhere to be found in the Lookup_vector) at the beginning of the last string of error values.

Note that Microsoft says "might not return the correct value" - basically, they're saying "all bets are off - and we might change this behavior in the next version of Excel without notice".

I've always used VLOOKUP or occasionally HLOOKUP and never LOOKUP; it's intriguing to think of using it as he did here - with constructed values instead of simple ranges of cells as you would V/HLOOKUP. And chances are it will continue to work as shown here through multiple versions of Excel.


2017-02-07 08:56:53

Michael (Micky) Avidan

@Bryce,
The following formula (presented in the above tip) returns the secon-to-last-value in column "A":

=INDEX(A:A,LOOKUP(2,1/(A:A<>""),ROW(A:A))-1)

*** Not all VBA command have a parallel sheet function
--------------------------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2017)
ISRAEL


2017-02-06 21:34:47

Bryce

@Micky
Yes, offset as required, I'm usually finding the next available line rather than second last. Haven't found the equivalent in formula yet that works from the bottom up. Some very useful tips here.


2017-02-06 10:26:27

Michael (Micky) Avidan

@Bryce,
According to this tip subject - I assume you meant:
Second_To_Last = Cells(Rows.Count, 1).End(xlUp).Offset(-1, 0)
--------------------------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2017)
ISRAEL


2017-02-05 22:58:54

Bryce

I usually use:

Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select

to get to the last line.

Gets there from the bottom of the sheet going up to the first used cell, then offsets for selection.


2017-02-05 13:19:17

Yvan Loranger

Another way:
b1=isblank(a1)
& replicate b1 down as far as needed.
Have d5 or whatever cell
=INDIRECT(ADDRESS(MATCH("TRUE",B1:B456,0)-2,1,1))
The -2 is to backtrack from 1st empty cell up to last value in column then up to 2nd-last value.


2017-02-04 05:47:12

Alex B

If you use an Excel Table then the last option is the way to go.
=INDEX(MyData,ROWS(MyData)-1)

The table will automatically expand the range.
The table version will looks a more like this:
=INDEX(Table1[COL2],ROWS(Table1[COL2])-1)


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.

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.