Selecting the last value of a column

前端 未结 23 1936
再見小時候
再見小時候 2020-11-28 19:15

I have a spreadsheet with some values in column G. Some cells are empty in between, and I need to get the last value from that column into another cell.

Something li

23条回答
  •  难免孤独
    2020-11-28 19:41

    Summary:

    =INDEX( FILTER( G2:G , NOT(ISBLANK(G2:G))) , COUNTA(G2:G) )
    

    Details:

    I've looked through and tried several answers, and here's what I've found: The simplest solution (see Dohmoose' answer) works if there are no blanks:

    =INDEX(G2:G; COUNT(G2:G))
    

    If you have blanks, it fails.

    You can handle one blank by just changing from COUNT to COUNTA (See user3280071's answer):

    =INDEX(G2:G; COUNTA(G2:G))
    

    However, this will fail for some combinations of blanks. (1 blank 1 blank 1 fails for me.)

    The following code works (See Nader's answer and jason's comment):

    =INDEX( FILTER( G2:G , NOT(ISBLANK(G2:G))) , ROWS( FILTER( G2:G , NOT(ISBLANK(G2:G)) ) ) )
    

    but it requires thinking about whether you want to use COLUMNS or ROWS for a given range.

    However, if COLUMNS is replaced with COUNT I seem to get a reliable, blank-proof implementation of LAST:

    =INDEX( FILTER( G2:G , NOT(ISBLANK(G2:G))) , COUNT( FILTER( G2:G , NOT(ISBLANK(G2:G)) ) ) ) 
    

    And since COUNTA has the filter built in, we can simplify further using

    =INDEX( FILTER( G2:G , NOT(ISBLANK(G2:G))) , COUNTA(G2:G) )
    

    This is somewhat simple, and correct. And you don't have to worry about whether to count rows or columns. And unlike script solutions, it automatically updates with changes to the spreadsheet.

    And if you want to get the last value in a row, just change the data range:

    =INDEX( FILTER( A2:2 , NOT(ISBLANK(A2:2))) , COUNTA(A2:2) )
    

提交回复
热议问题