I was just wondering if you could help me better understand what .Cells(.Rows.Count,"A").End(xlUp).row
does. I understand the portion before the .End
part.
Item(row, column) to identify one specific cell. So, now we know what Cells (Rows. Count, 1) means – the last cell in column 1, i.e., column A. In the current version of XL that would be A65536, but it could be something else in a future version.
The VBA snippet End(xlup). Row will find the last used row in an Excel range. Knowing the last row in Excel is useful for looping through columns of data.
Rows. Count returns the total count of rows in the worksheet (1048576 in Excel 2010). .Cells(.Rows.Count, "A") returns the bottom most cell in column "A" in the worksheet.
The Rows. Count statement returns a count of all the rows in the worksheet. Therefore, we are basically specifying the last cell in column A of the sheet (cell A1048567), and going up until we find the first non-blank cell. It works the same with finding the last column.
It is used to find the how many rows contain data in a worksheet that contains data in the column "A". The full usage is
lastRowIndex = ws.Cells(ws.Rows.Count, "A").End(xlUp).row
Where ws
is a Worksheet object. In the questions example it was implied that the statement was inside a With
block
With ws lastRowIndex = .Cells(.Rows.Count, "A").End(xlUp).row End With
ws.Rows.Count
returns the total count of rows in the worksheet (1048576 in Excel 2010)..Cells(.Rows.Count, "A")
returns the bottom most cell in column "A" in the worksheetThen there is the End
method. The documentation is ambiguous as to what it does.
Returns a Range object that represents the cell at the end of the region that contains the source range
Particularly it doesn't define what a "region" is. My understanding is a region is a contiguous range of non-empty cells. So the expected usage is to start from a cell in a region and find the last cell in that region in that direction from the original cell. However there are multiple exceptions for when you don't use it like that:
rng.cells(1,1)
. So Range.End
is not a trivial function.
.row
returns the row index of that cell.[A1].End(xlUp) [A1].End(xlDown) [A1].End(xlToLeft) [A1].End(xlToRight)
is the VBA equivalent of being in Cell A1 and pressing Ctrl + Any arrow key. It will continue to travel in that direction until it hits the last cell of data, or if you use this command to move from a cell that is the last cell of data it will travel until it hits the next cell containing data.
If you wanted to find that last "used" cell in Column A, you could go to A65536 (for example, in an XL93-97 workbook) and press Ctrl + Up to "snap" to the last used cell. Or in VBA you would write:
Range("A65536").End(xlUp)
which again can be re-written as Range("A" & Rows.Count).End(xlUp)
for compatibility reasons across workbooks with different numbers of rows.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With