Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of .Cells(.Rows.Count,"A").End(xlUp).row

Tags:

excel

vba

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.

like image 241
rmdlp Avatar asked Nov 21 '14 16:11

rmdlp


People also ask

What does cells rows count 1 mean?

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.

What is end XLUP .row in VBA?

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.

What means row count?

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.

What is rows count in VBA?

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.


2 Answers

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 
  1. ws.Rows.Count returns the total count of rows in the worksheet (1048576 in Excel 2010).
  2. .Cells(.Rows.Count, "A") returns the bottom most cell in column "A" in the worksheet

Then 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:

  • If the range is multiple cells, it will use the region of rng.cells(1,1).
  • If the range isn't in a region, or the range is already at the end of the region, then it will travel along the direction until it enters a region and return the first encountered cell in that region.
  • If it encounters the edge of the worksheet it will return the cell on the edge of that worksheet.

So Range.End is not a trivial function.

  1. .row returns the row index of that cell.
like image 70
cheezsteak Avatar answered Oct 12 '22 02:10

cheezsteak


[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.

like image 34
SierraOscar Avatar answered Oct 12 '22 01:10

SierraOscar