Get the last non-empty cell in a column in Google Sheets

后端 未结 16 2194
温柔的废话
温柔的废话 2020-11-28 21:04

I use the following function

=DAYS360(A2, A35)

to calculate the difference between two dates in my column. However, the column is ever exp

相关标签:
16条回答
  • 2020-11-28 21:33

    There may be a more eloquent way, but this is the way I came up with:

    The function to find the last populated cell in a column is:

    =INDEX( FILTER( A:A ; NOT( ISBLANK( A:A ) ) ) ; ROWS( FILTER( A:A ; NOT( ISBLANK( A:A ) ) ) ) )
    

    So if you combine it with your current function it would look like this:

    =DAYS360(A2,INDEX( FILTER( A:A ; NOT( ISBLANK( A:A ) ) ) ; ROWS( FILTER( A:A ; NOT( ISBLANK( A:A ) ) ) ) ))
    
    0 讨论(0)
  • 2020-11-28 21:33

    This will give the contents of the last cell:

    =indirect("A"&max(ARRAYFORMULA(row(a:a)*--(a:a<>""))))
    

    This will give the address of the last cell:

    ="A"&max(ARRAYFORMULA(row(a:a)*--(a:a<>"")))
    

    This will give the row of the last cell:

    =max(ARRAYFORMULA(row(a:a)*--(a:a<>"")))
    

    Maybe you'd prefer a script. This script is way shorter than the huge one posted above by someone else:

    Go to script editor and save this script:

    function getLastRow(range){
      while(range.length>0 && range[range.length-1][0]=='') range.pop();
      return range.length;
    }
    

    One this is done you just need to enter this in a cell:

    =getLastRow(A:A)
    
    0 讨论(0)
  • 2020-11-28 21:39

    This works for me. Get last value of the column A in Google sheet:

    =index(A:A,max(row(A:A)*(A:A<>"")))
    

    (It also skips blank rows in between if any)

    0 讨论(0)
  • 2020-11-28 21:40

    My favorite is:

    =INDEX(A2:A,COUNTA(A2:A),1)
    

    So, for the OP's need:

    =DAYS360(A2,INDEX(A2:A,COUNTA(A2:A),1))
    
    0 讨论(0)
  • 2020-11-28 21:41

    If the column expanded only by contiguously added dates as in my case - I used just MAX function to get last date.

    The final formula will be:

    =DAYS360(A2; MAX(A2:A)) 
    
    0 讨论(0)
  • 2020-11-28 21:44

    Here's another one:

    =indirect("A"&max(arrayformula(if(A:A<>"",row(A:A),""))))
    

    With the final equation being this:

    =DAYS360(A2,indirect("A"&max(arrayformula(if(A:A<>"",row(A:A),"")))))
    

    The other equations on here work, but I like this one because it makes getting the row number easy, which I find I need to do more often. Just the row number would be like this:

    =max(arrayformula(if(A:A<>"",row(A:A),"")))
    

    I originally tried to find just this to solve a spreadsheet issue, but couldn't find anything useful that just gave the row number of the last entry, so hopefully this is helpful for someone.

    Also, this has the added advantage that it works for any type of data in any order, and you can have blank rows in between rows with content, and it doesn't count cells with formulas that evaluate to "". It can also handle repeated values. All in all it's very similar to the equation that uses max((G:G<>"")*row(G:G)) on here, but makes pulling out the row number a little easier if that's what you're after.

    Alternatively, if you want to put a script on your sheet you can make it easy on yourself if you plan on doing this a lot. Here's that scirpt:

    function lastRow(sheet,column) {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      if (column == null) {
        if (sheet != null) {
           var sheet = ss.getSheetByName(sheet);
        } else {
          var sheet = ss.getActiveSheet();
        }
        return sheet.getLastRow();
      } else {
        var sheet = ss.getSheetByName(sheet);
        var lastRow = sheet.getLastRow();
        var array = sheet.getRange(column + 1 + ':' + column + lastRow).getValues();
        for (i=0;i<array.length;i++) {
          if (array[i] != '') {       
            var final = i + 1;
          }
        }
        if (final != null) {
          return final;
        } else {
          return 0;
        }
      }
    }
    

    Here you can just type in the following if you want the last row on the same of the sheet that you're currently editing:

    =LASTROW()
    

    or if you want the last row of a particular column from that sheet, or of a particular column from another sheet you can do the following:

    =LASTROW("Sheet1","A")
    

    And for the last row of a particular sheet in general:

    =LASTROW("Sheet1")
    

    Then to get the actual data you can either use indirect:

    =INDIRECT("A"&LASTROW())
    

    or you can modify the above script at the last two return lines (the last two since you would have to put both the sheet and the column to get the actual value from an actual column), and replace the variable with the following:

    return sheet.getRange(column + final).getValue();
    

    and

    return sheet.getRange(column + lastRow).getValue();
    

    One benefit of this script is that you can choose if you want to include equations that evaluate to "". If no arguments are added equations evaluating to "" will be counted, but if you specify a sheet and column they will now be counted. Also, there's a lot of flexibility if you're willing to use variations of the script.

    Probably overkill, but all possible.

    0 讨论(0)
提交回复
热议问题