vba .Cells(.Rows.Count,"A").End(xlUp).row 的含义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27065840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Meaning of .Cells(.Rows.Count,"A").End(xlUp).row
提问by rmdlp
I was just wondering if you could help me better understand what .Cells(.Rows.Count,"A").End(xlUp).rowdoes. I understand the portion before the .Endpart.
我只是想知道你是否能帮助我更好地理解是什么.Cells(.Rows.Count,"A").End(xlUp).row。我理解部分之前的.End部分。
回答by cheezsteak
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
它用于查找包含“A”列中数据的工作表中有多少行包含数据。完整的用法是
lastRowIndex = ws.Cells(ws.Rows.Count, "A").End(xlUp).row
Where wsis a Worksheet object. In the questions example it was implied that the statement was inside a Withblock
ws工作表对象在哪里。在问题示例中,暗示该语句位于With块内
With ws
lastRowIndex = .Cells(.Rows.Count, "A").End(xlUp).row
End With
ws.Rows.Countreturns 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
ws.Rows.Count返回工作表中的总行数(Excel 2010 中的 1048576)。.Cells(.Rows.Count, "A")返回工作表中“A”列中最底部的单元格
Then there is the Endmethod. The documentationis ambiguous as to what it does.
然后是End方法。该文档对它的作用不明确。
Returns a Range object that represents the cell at the end of the region that contains the source range
返回一个 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.
- 如果范围是多个单元格,它将使用
rng.cells(1,1). - 如果范围不在某个区域内,或者该范围已经在该区域的末尾,则它将沿该方向行进,直到进入某个区域并返回该区域中遇到的第一个单元格。
- 如果它遇到工作表的边缘,它将返回该工作表边缘的单元格。
So Range.Endis not a trivial function.
所以Range.End不是一个微不足道的功能。
.rowreturns the row index of that cell.
.row返回该单元格的行索引。
回答by Sam
[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 isthe last cell of data it will travel until it hits the next cell containing data.
是在单元格 A1 中并按Ctrl+ 任意箭头键的 VBA 等效项。它将继续行进在那个方向,直到达到数据的最后一个单元格,或者如果你使用这个命令从一个细胞移动是它将行进,直到它命中包含数据的下一个单元格数据的最后一个单元格。
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:
如果您想在 A 列中找到最后一个“已使用”的单元格,您可以转到 A65536(例如,在 XL93-97 工作簿中)并按 Ctrl + Up 以“对齐”到最后一个使用的单元格。或者在 VBA 中你会写:
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.
Range("A65536").End(xlUp)Range("A" & Rows.Count).End(xlUp)出于兼容性原因,可以重新编写具有不同行数的工作簿。
回答by rpalo
The first part:
第一部分:
.Cells(.Rows.Count,"A")
Sends you to the bottom row of column A, which you knew already.
将您发送到您已经知道的 A 列的底行。
The End function starts at a cell and then, depending on the direction you tell it, goes that direction until it reaches the edge of a group of cells that have text. Meaning, if you have text in cells C4:E4 and you type:
End 函数从一个单元格开始,然后根据您告诉它的方向,沿着该方向前进,直到到达一组包含文本的单元格的边缘。意思是,如果您在单元格 C4:E4 中有文本并键入:
Sheet1.Cells(4,"C").End(xlToRight).Select
The program will select E4, the rightmost cell with text in it.
程序将选择 E4,最右边的单元格,其中包含文本。
In your case, the code is spitting out the row of the very last cell with text in it in column A. Does that help?
在您的情况下,代码吐出最后一个单元格的行,其中包含 A 列中的文本。这有帮助吗?
回答by user5331486
.Cells(.Rows.Count,"A").End(xlUp).row
I think the first dot in the parenthesis should not be there, I mean, you should write it in this way:
我认为括号中的第一个点不应该在那里,我的意思是,你应该这样写:
.Cells(Rows.Count,"A").End(xlUp).row
Before the Cells, you can write your worksheet name, for example:
在单元格之前,您可以写下您的工作表名称,例如:
Worksheets("sheet1").Cells(Rows.Count, 2).End(xlUp).row
The worksheet name is not necessary when you operate on the same worksheet.
当您在同一工作表上操作时,工作表名称不是必需的。

