INDEX with COUNTA misses the last row when the column has blanks — Google Sheets fix
Updated
A common pattern for “the last value in column A” is =INDEX(A:A,COUNTA(A:A)). It works on a short list with no holes. As soon as someone leaves a blank in the middle, COUNTA is smaller than the row you want, and INDEX returns an earlier cell — here 102 instead of 103 — with no error triangle.
This page is that last-row pattern. What COUNTA treats as present — including "" and 0 — is COUNTA hidden zero. INDEX with row 0, which spills the whole column, is INDEX row 0. INDEX past a short range is INDEX #REF!. Keep the Google Sheets UI in English.
Recreate it on a blank sheet
Section titled “Recreate it on a blank sheet”- Open a blank spreadsheet. In A1 type
sku. In A2 type101. Leave A3 untouched. In A4 type102. In A5 type103. The last sku is on row 5. There is a gap on row 3.
COUNTA is not 5. In C1 type counta. In C2 enter =COUNTA(A:A). C2 is 4 (header plus three skus). The empty A3 is not counted. Leave C2 as 4.
INDEX of that count misses the last sku. In C4 type index_counta. In C5 enter =INDEX(A:A,COUNTA(A:A)). C5 is 102, the value on row 4, not 103 on row 5. COUNTA was 4, so INDEX returned A4. There is no triangle. Leave C5 as 102.
A range that starts under the header still fails. In C7 enter =INDEX(A2:A,COUNTA(A2:A)). COUNTA(A2:A) is 3. Relative row 3 of A2:A is A4, still 102. The header is no longer in the count, and the last sku is still missed. Leave C7 as 102.
The last value, as control. In E1 type last. In E2 enter =INDEX(A:A,MAX(FILTER(ROW(A:A),A:A<>""))). E2 is 103. Leave E2 as the control. A similar XLOOKUP from last to first, =XLOOKUP(TRUE,A:A<>"",A:A,"",0,-1), returned #N/A on the live sheet used for these screenshots, so it is not the published fix. If you already use XLOOKUP that way, confirm it on your file; see XLOOKUP last to first.
A formula blank is counted, but it does not automatically land you on that cell. COUNTA does count ="", as COUNTA hidden zero explains. On this layout, after A8 is =IF(FALSE,"x",""), COUNTA(A:A) becomes 5, and INDEX(A:A,5) is still A5 (103), not the formula on row 8. That overshoot recreate is not shown here, because those steps do not produce a blank-looking result.
A tight list with no gaps, why the pattern survives. In G1 type tight. G2 201. G3 202. G4 203. In G6 enter =INDEX(G:G,COUNTA(G:G)). G6 is 203. The count is 4 including the header, and row 4 is the last value. This is why the formula looks fine on a clean sample and fails on a real column.
What is actually wrong
Section titled “What is actually wrong”COUNTA(range)returns how many cells are not truly empty. It is not the row number of the last filled cell. Those two numbers match only when the filled cells are packed at the top of the range with no gaps.INDEX(A:A, n)returns row n of the spreadsheet when the range is the whole column. IfnisCOUNTA(A:A)and A3 is blank,nis too small and you get a cell above the last value (102instead of103). A formula blank below the data is counted, but the index only lands on that later row when the count happens to equal that row number.- Starting at A2 does not repair a gap.
INDEX(A2:A,COUNTA(A2:A))counts from A2, but a hole still shortens the count relative to the last row. COUNTAof a whole column also includes the header. On a packed list that can accidentally be the right row number (header + k values = last row). That coincidence is G6, not a definition.0and a single space are present forCOUNTA. A “blank” that is actually0will be counted and can also shift the index. See COUNTA hidden zero.INDEX(A2:A10, COUNTA(A2:A10))can become#REF!if the count is larger than the height of that short range (for example after formula blanks you did not include in the INDEX range). That tooltip is INDEX #REF!. The openA:Aform usually returns a blank cell instead of#REF!.
This is not INDEX row 0. Row 0 means “the whole column.” This page’s formula uses a positive COUNTA result.
This is not LOOKUP approximate. The last-row LOOKUP(2,1/(A:A<>""),A:A) trick is a different technique; if you use it, know that LOOKUP is approximate. Prefer XLOOKUP or the MAX/FILTER/ROW form above.
Leave C5 as 102 so the missed last row stays visible.
Last non-empty value. Keep E2: =INDEX(A:A,MAX(FILTER(ROW(A:A),A:A<>""))). A:A<>"" is FALSE on a true blank and on "", so a formula blank drops out and A5 remains the last sku.
Last non-empty row number. =MAX(FILTER(ROW(A:A),A:A<>"")) is the row itself, if you need the number for INDEX of another column: =INDEX(B:B,MAX(FILTER(ROW(A:A),A:A<>""))).
Stop filling unused rows with ="". If a column formula must exist, have it return a true empty result only for rows you want COUNTA and this last-row pattern to ignore, or stop using COUNTA as a row index.
Do not wrap C5 in IFERROR. The cell is 102. That is a real sku, just the wrong one. See IFERROR hides cause.
If INDEX plus COUNTA returns a value from the middle of the column, there is a gap (or a header you did not account for). If it returns the last value on a packed list, that is coincidence, not a reason to keep the pattern on a column that will grow holes.