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 columnToCheck As Long = 1) As Long
Dim ws As Worksheet
Set ws = Worksheets(wsName)
lastRow = ws.Cells(ws.Rows.Count, columnToCheck).End(xlUp).Row
End Function
For the last row there are two excel formulas I found so far:
=SUMPRODUCT(MAX((A:A<>"")*ROW(A:A)))
=MAX(IFERROR(MATCH(E1+99,A:A),0),IFERROR(MATCH("zzz",A:A),0))
The problem with them is that the first does not work, if there is an error in the range and the second does not return the real last row, if it is an error, but the last non-error row, thus returning 6 and not 7:
The question is how to get a formula that returns 7, as this is the last row in column A that is used? Although, it is an error. Without VBA.
This one (without an array formula)
How does it work?
NOT(ISBLANK(A:A))
returns an array ofTrue
andFalse
1/(NOT(ISBLANK(A:A)))
then 1 divided by that array results in another array of1
or#DIV/0!
which is used aslookup_vector
2
aslookup_value
which cannot be found because the largest value in thelookup_array
is1
, so lookup will match the last1
in the array.result_vector
we useROW(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).Try the following formula:
Remember that this formula needs to be entered with control+shift+enter.
(A:A) <> ""
: checks for not empty cells and returnsTRUE
if not empty.(IFERROR(((A:A)<>"");TRUE)
: checks for not empty cells and returnsTRUE
if not empty, if an error is detected it returns alsoTRUE
.Rest of code works as before.
More info about the control+shift+enter combination can be found here: Guidelines and examples of array formulas