How-to recipes · Lookups

How to find the last value in a column in Excel

Return the last non-empty value of a column, even with gaps, with TAKE and TOCOL or XLOOKUP in Microsoft 365, or LOOKUP(2,1/...) in older Excel.

Intermediate XLOOKUP TAKE TOCOL LOOKUP INDEX MATCH COUNTA ROW

Many sheets grow downward: a meter reading every week, a bank balance after every transaction, the latest exchange rate pasted at the bottom each morning. A summary cell at the top should always show the most recent entry, without you editing the formula every time a row is added. Gaps make it harder: a week without a reading leaves an empty cell, so counting the filled cells does not tell you where the last one is. This recipe gives you formulas that find the last value reliably.

The example data

Weekly meter readings in B2:B8. Week 3 is missing and weeks 6 and 7 have not happened yet. The last reading is 1702 in row 6.

Microsoft 365: XLOOKUP searching from the bottom

XLOOKUP can search from the last row upwards when you set its search_mode argument to -1. Search for TRUE in an array that marks the non-empty cells:

D2fx=XLOOKUP(TRUE,B2:B8<>"",B2:B8,,0,-1)
ABCD
1WeekReading
2W115201702
3W21588
4W3
5W41650
6W51702
7W6
8W7

Starting at B8 and moving up, the first non-empty cell is B6.

Result: 1702

  • B2:B8<>"" returns TRUE for every filled cell.
  • 0 asks for an exact match and -1 searches last-to-first.
  • Change the third argument to A2:A8 to return the week of the last reading (W5) instead of the reading itself.

Alternative: TAKE + TOCOL

TOCOL with 1 as its second argument drops blank cells, and TAKE with -1 keeps the last item: =TAKE(TOCOL(B2:B8,1),-1) also returns 1702. It reads nicely, but note that TOCOL only ignores truly empty cells; a formula returning "" still counts as a value, while the XLOOKUP version above treats it as empty.

Excel 2016 and 2019: the LOOKUP(2,1/...) pattern

This formula looks strange, but it has worked in every version for decades and needs no Ctrl+Shift+Enter:

D2fx=LOOKUP(2,1/(B2:B8<>""),B2:B8)
ABCD
1WeekReading
2W115201702
3W21588
4W3
5W41650
6W51702
7W6
8W7

The classic last-value formula.

Result: 1702

  1. (B2:B8<>"") gives TRUE/FALSE for each cell.
  2. 1/... turns TRUE into 1 and FALSE into a #DIV/0! error: {1;1;#DIV/0!;1;1;#DIV/0!;#DIV/0!}.
  3. LOOKUP searches for 2, a number larger than anything in the array. It ignores errors and, because it cannot find 2, it settles on the last number it saw, which is the 1 in position 5.
  4. It returns position 5 of B2:B8: 1702.

Change the last argument to get related information: =LOOKUP(2,1/(B2:B8<>""),A2:A8) returns the week, and =LOOKUP(2,1/(B2:B8<>""),ROW(B2:B8)) returns the row number, here 6.

Numbers only: INDEX + MATCH with a huge number

If the column contains only numbers, =INDEX(B:B,MATCH(9.99E+307,B:B)) returns the last number in the column. MATCH in approximate mode looks for the largest possible number, fails, and stops at the last numeric cell. For text-only columns the same trick uses MATCH("zzzz",B:B). These are very fast, even on whole columns.

Why COUNTA is not enough

A common attempt is =INDEX(B:B,COUNTA(B:B)). COUNTA counts 5 filled cells (header plus four readings), so INDEX returns row 5, which holds 1650, the reading of week 4, not the last reading. It only works when the column has no gaps at all. With the gap in row 4, the answer is wrong without any warning.

Pitfalls

  • Formulas that return an empty text "" look blank but are not empty to TOCOL and COUNTA. The <>"" test in the XLOOKUP and LOOKUP formulas treats them as empty, which is usually what you want.
  • A cell containing a single space is not empty. If a value seems to be missed or a blank is returned, check for spaces with =LEN(B7).
  • Whole-column references like B:B inside array tests (1/(B:B<>"")) make Excel test a million cells and can slow the workbook. Use a reasonable range such as B2:B5000, or an Excel table.
  • If you store the data in a table (Excel tables and structured references), the formula =XLOOKUP(TRUE,Readings[Value]<>"",Readings[Value],,0,-1) grows with the table automatically.
Tip. To get the last row number in VBA instead of a formula, see Find the last row and build dynamic ranges in VBA. To return the second, third or n-th match instead of the last one, see How to get the nth match in Excel.
⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 14 KB

More how-to recipes