I will try elaborate already given answer.
To find last row/column, there's End
method of a Range
object. I.e. if you have Cells(row, column)
, you then can append to it .End()
which can have four arguments: xlUp, xlDown, xlToLeft, xlToRight
. This function returns a cell (so Range
object), so to have row/column number, you need to acces Row
/ Column
property. To sum up, you should use:
Cells(row, column).End(X).Row
Cells(row, column).End(X).Column
where row
, column
are integers and X
is one of mentioned parameters.
To find last row, you should use specific column number (for column that you want to know last row). You have two options there:
xlDown
- you can think of it like this: start in a given cell, then go down until first blank cell is met. Return last non-empty cell.
xlUp
- start in given cell, then go all the way up until non-blank cell is met.
Note: if you start with empty cell, End
method will return first non-empty cell. On the other hand, if you start with non-empty cell, the End
method will go until first empty cell is met and return last non-empty cell.
It all sound complicated, but let me give some examples:
let's have a sheet:
A
1 1
2 2
3
4 4
5 5
then
MsgBox Cells(1, 1).End(xlDown).Row 'returns 2
MsgBox Cells(5, 1).End(xlUp).Row 'returns 4
'start with cell in last row
MsgBox Cells(Rows.Count, 1).End(xlUp).Row 'returns 5