Excel VBA: .Range & Cells(row.count xlup
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41176877/
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
Excel VBA: .Range & Cells(row.count xlup
提问by Phil S.
I am trying to understand the following line:
我试图理解以下行:
.Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row).ClearContents
Can someone tell what .Range("A1:A" &means?
有人能说出.Range("A1:A" &是什么意思吗?
From my research so far I found the following:
从我到目前为止的研究中,我发现了以下几点:
- Cellsrefers to the cells
- .Rows.Countis a function that returns the number of rows in the table (=65536)
- , "A"refers to the column you want to search (here A)
- .Endtells EXCEL where to start
- xlUpthe direction to which EXCEL should search
- .Rowthe first cell from the bottom that is not empty
- .ClearContentsdeletes all values
- 细胞是指细胞
- .Rows.Count是一个返回表中行数的函数(=65536)
- ,“A”指的是你要搜索的列(这里是A)
- .End告诉 EXCEL 从哪里开始
- xlUpEXCEL 应搜索的方向
- .Row底部第一个不为空的单元格
- .ClearContents删除所有值
Can someone tell if this is corrrect, and overall explain what the entire line is trying to do?
有人可以判断这是否正确,并总体解释整条生产线试图做什么?
Assuming there may be values later added to column A, but I only wanted to delete the contents of 6 cells (A50:A55) how would I change this?
假设稍后可能有值添加到 A 列,但我只想删除 6 个单元格 (A50:A55) 的内容,我该如何更改?
回答by YowE3K
.Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row).ClearContents
Breaking the line down (you had most of it right):
打破界限(你大部分都说对了):
The first thing to notice is the
.
at the start of many of the Properties/Methods. That tells us that the code must be within aWith
block, probably something likeWith Worksheets("Sheet1")
or an equivalent. So everything starting with a.
is just a shortcut to sayWorksheets("Sheet1").
..Rows
returns all theRows
in the worksheet, and.Rows.Count
is therefore the count of how many rows there are. In older versions of Excel it was 65,536, and in newer versions it is 1,048,576..Cells(.Rows.Count, "A")
returns an object which refers to the last cell in column A, e.g. the Cell located at $A$65536.End(xlUp)
says to go from that cell and find the previous non-empty cell in an upward direction. (If cell $A$65536 was non-empty, it would do something different, but I will gloss over that.).Row
then finds the row number of that non-empty cell. For the sake of this explanation, let us pretend that was cell $A$5201 so.Cells(.Rows.Count, "A").End(xlUp).Row
will return5201
"A1:A" & 5201
will concatenate"A1:A"
with"5201"
(implicit type conversion from number to string) to generate the string"A1:A5201"
Range("A1:A5201").ClearContents
will clear the contents of the cellsA1:A5201
首先要注意的是
.
许多属性/方法的开头。这告诉我们代码必须在一个With
块内,可能是类似的With Worksheets("Sheet1")
或等价的。所以所有以 a 开头的.
都只是一个快捷方式Worksheets("Sheet1").
。.Rows
返回Rows
工作表中的所有,.Rows.Count
因此是有多少行的计数。在旧版本的 Excel 中,它是 65,536,而在较新版本中,它是 1,048,576。.Cells(.Rows.Count, "A")
返回一个对象,该对象引用 A 列中的最后一个单元格,例如位于 $A$65536 的单元格.End(xlUp)
说从那个单元格开始,向上找到前一个非空单元格。(如果单元格 $A$65536 非空,它会做一些不同的事情,但我会掩盖它。).Row
然后找到该非空单元格的行号。为了便于说明,让我们假设这是单元格 $A$5201,因此.Cells(.Rows.Count, "A").End(xlUp).Row
将返回5201
"A1:A" & 5201
将"A1:A"
与"5201"
(从数字到字符串的隐式类型转换)连接以生成字符串"A1:A5201"
Range("A1:A5201").ClearContents
将清除单元格的内容A1:A5201