Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. 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: Referring to the Last Cell.

Referring to the Last Cell

Written by Allen Wyatt (last updated December 10, 2022)

4

There may be times when you are putting together a workbook and you want to keep a summary on one worksheet and detail information on another. For instance, let's suppose Sheet1 is your summary worksheet, and you have detailed information for bank accounts on Sheet2. In looking at the detail information, you have dates in column A, and balances for different accounts in columns B, C, and D. Thus, the detailed information is a table that shows a running progression of bank balances on different dates.

In putting together your summary information on Sheet1, you realize that you need to reference the last figures in columns B, C, and D. These figures represent the latest balances, and thus are perfect for your summary. How do you do it? Particularly when you continue to add information to your detail worksheet over time?

Actually, there are several ways to approach the problem. (There are typically several ways to solve any Excel problem.) One way is to use the VLOOKUP function. At the point in the summary where you want the latest balance from column B of the detail (Sheet2), you would put the following formula:

=VLOOKUP(MAX(Sheet2!$A:$A),Sheet2!$A:$D,2)

To change references for the other two account balances, you would simply change the last number (2) to either 3 (for the account in column C) or 4 (for the account in column D). The function works because it looks up the maximum value in column A, which contains dates. It then looks in the data table (Sheet2!$A:$D) and finds the appropriate offset for the desired column.

This approach works fine, provided there are no dates in column A past the last balances entered. If there are, then the values returned will always be incorrect.

Another way to approach the problem is to use the INDEX function in conjunction with either COUNT or COUNTA. If the detail columns don't contain any text (even in the column headers), then you would use the COUNT function. If there is text included, then COUNTA is preferred. At the point where you want to include the last balance from column B of the detail, you would use the following formula:

=INDEX(Sheet2!B:B,COUNTA(Sheet2!B:B))

It looks into the table, determines the number of non-blank cells in column B, and then pulls the figure from that last non-blank cell. To adapt the formula for columns C and D, simply change the B references to the appropriate C or D.

Still another way to deal with the problem is to use the OFFSET function, as in the following:

=OFFSET(Sheet2!B1,COUNTA(Sheet2!B:B)-1,0)

This function returns the value of a cell offset from a base reference cell. In this case, the base cell is Sheet2!B1. The COUNTA function is used to determine how many rows to offset from the base, and the 0 specifies that the offset should be in the same column as the base reference. To change the formula for columns C and D, simply change all references to B to either C or D.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12470) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Referring to the Last Cell.

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

Moving a Table Row

Want to move a row in a table very easily? You can do so by using the same editing techniques you are already using.

Discover More

Adding Hyperlinks

Adding a hyperlink to s text selection is easy to do in Word. All you need to do is make a couple of clicks and specify ...

Discover More

Highlighting Duplicate Words

One way to help improve your writing is to minimize the number of duplicated words you use in your prose. Depending on ...

Discover More

Program Successfully in Excel! This guide will provide you with all the information you need to automate any task in Excel and save time and effort. Learn how to extend Excel's functionality with VBA to create solutions not possible with the standard features. Includes latest information for Excel 2024 and Microsoft 365. Check out Mastering Excel VBA Programming today!

More ExcelTips (ribbon)

How Many Times Does Each Name Appear in a List?

If you have a list of names in a column, and you want to know how many times those names appear in a larger list of data, ...

Discover More

Summing the Largest Values

If you want to calculate the sum of the largest positive and negative values in a column, there are multiple ways you can ...

Discover More

Returning the Rightmost Value in a Row

Do you need to figure out the rightmost value within a row in which not all cells may contain values? This tip provides a ...

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 seven minus 1?

2022-12-18 09:54:47

J. Woolley

Apologies for the poor formatting in my previous comment, making it almost unreadable. I tried to submit my comment over several days but it was repeatedly blocked as spam. Each time I clicked the option to request review. Apparently it was finally reviewed and accepted, but the line breaks did not survive.
I wish I knew why some comments are interpreted as spam. However, my compliments to Allen for publishing these Tips and permitting these comments.


2022-12-18 07:32:38

Alex Blakenburg

One of the things I love about XLOOKUP is that it doesn't just return the value, it returns the Cell, so you can use it to define the range in another function or in the Names Manager. Index in combination with the new XMATCH would work the same way.


2022-12-17 10:21:10

J. Woolley

The goal is the value of the last non-blank cell in column B of Sheet2. Alex Blakenburg's formula is very clever:
=XLOOKUP(TRUE,Sheet2!B:B<>"",Sheet2!B:B,"",0,-1)
Sheet2!B:B<>"" is a TRUE/FALSE array of all non-blank cells in column B. The formula searches that array in the reverse direction (bottom-up) for the first TRUE and returns the value from the corresponding row in column B (or "" if TRUE is not found). Here is a similar formula based on a TRUE/FALSE array of all blank cells in column B:
=XLOOKUP(FALSE,ISBLANK(Sheet2!B:B),Sheet2!B:B,"",0,-1)
Modern versions of Excel have 1,048,576 rows. These formulas first populate an array of that size, then evaluate that array starting at the last item (number 1,048,576) and working toward the first item.
Here are two forms of the traditional formula that work in any version of Excel:
=LOOKUP(2,1/(Sheet2!B:B<>""),Sheet2!B:B)
=LOOKUP(2,1/NOT(ISBLANK(Sheet2!B:B)),Sheet2!B:B)
These evaluate in the top-down direction, so they might be faster. For details, see
https://exceljet.net/formulas/get-value-of-last-non-empty-cell
And here are two formulas that use SUMPRODUCT, but they might be slower:
=INDEX(Sheet2!B:B,SUMPRODUCT(MAX((Sheet2!B:B<>"")*ROW(Sheet2!B:B))))
=INDEX(Sheet2!B:B,SUMPRODUCT(MAX(NOT(ISBLANK(Sheet2!B:B))*ROW(Sheet2!B:B))))
If you know the pratical limit for non-blank rows in Sheet2 column B is less than 10,000, for example, you might replace Sheet2!B:B in these formulas with Sheet2!B1:B9999. This would correspond to over 27 years of daily entries.


2022-12-10 07:53:38

Alex Blakenburg

If you have MS365 or Excel 2021 XLOOKUP is a much more reliable way of getting the last row, eg
=XLOOKUP(TRUE, Sheet2!B:B<>"", Sheet2!B:B, "", 0, -1)

The functions using COUNTA assume your data starts in Row 1 and that every row is populated.


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.