Get the last row of a column in Excel, without VBA

前端 未结 2 1764
醉酒成梦
醉酒成梦 2020-12-21 17:00

In general, getting the last row of a given column in Excel is easily done with a User-Defined Function in VBA:

Function lastRow(wsName As String, Optional c         


        
相关标签:
2条回答
  • 2020-12-21 17:26

    This one (without an array formula)

    =LOOKUP(2,1/(NOT(ISBLANK(A:A))),ROW(A:A)) 
    

    How does it work?

    1. NOT(ISBLANK(A:A)) returns an array of True and False
    2. 1/(NOT(ISBLANK(A:A))) then 1 divided by that array results in another array of 1 or #DIV/0! which is used as lookup_vector
    3. We use 2 as lookup_value which cannot be found because the largest value in the lookup_array is 1, so lookup will match the last 1 in the array.
    4. As result_vector we use ROW(A:A) to get the row number of the last used cell
      (alternatively use A:A to get the value instead of the row number).
    0 讨论(0)
  • 2020-12-21 17:32

    Try the following formula:

    =SUMPRODUCT((MAX(IFERROR(((A:A)<>"");TRUE)*ROW(A:A))))
    

    Remember that this formula needs to be entered with control+shift+enter.

    (A:A) <> "" : checks for not empty cells and returns TRUE if not empty.
    (IFERROR(((A:A)<>"");TRUE) : checks for not empty cells and returns TRUE if not empty, if an error is detected it returns also TRUE.

    Rest of code works as before.

    More info about the control+shift+enter combination can be found here: Guidelines and examples of array formulas

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