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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:42:52  来源:igfitidea点击:

Excel VBA: .Range & Cells(row.count xlup

excelvbaexcel-vba

提问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:

从我到目前为止的研究中,我发现了以下几点:

  1. Cellsrefers to the cells
  2. .Rows.Countis a function that returns the number of rows in the table (=65536)
  3. , "A"refers to the column you want to search (here A)
  4. .Endtells EXCEL where to start
  5. xlUpthe direction to which EXCEL should search
  6. .Rowthe first cell from the bottom that is not empty
  7. .ClearContentsdeletes all values
  1. 细胞是指细胞
  2. .Rows.Count是一个返回表中行数的函数(=65536)
  3. ,“A”指的是你要搜索的列(这里是A)
  4. .End告诉 EXCEL 从哪里开始
  5. xlUpEXCEL 应搜索的方向
  6. .Row底部第一个不为空的单元格
  7. .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):

打破界限(你大部分都说对了):

  1. 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 a Withblock, probably something like With Worksheets("Sheet1")or an equivalent. So everything starting with a .is just a shortcut to say Worksheets("Sheet1")..

  2. .Rowsreturns all the Rowsin the worksheet, and .Rows.Countis 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.

  3. .Cells(.Rows.Count, "A")returns an object which refers to the last cell in column A, e.g. the Cell located at $A$65536

  4. .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.)

  5. .Rowthen 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).Rowwill return 5201

  6. "A1:A" & 5201will concatenate "A1:A"with "5201"(implicit type conversion from number to string) to generate the string "A1:A5201"

  7. Range("A1:A5201").ClearContentswill clear the contents of the cells A1:A5201

  1. 首先要注意的是.许多属性/方法的开头。这告诉我们代码必须在一个With块内,可能是类似的With Worksheets("Sheet1")或等价的。所以所有以 a 开头的.都只是一个快捷方式Worksheets("Sheet1").

  2. .Rows返回Rows工作表中的所有,.Rows.Count因此是有多少行的计数。在旧版本的 Excel 中,它是 65,536,而在较新版本中,它是 1,048,576。

  3. .Cells(.Rows.Count, "A")返回一个对象,该对象引用 A 列中的最后一个单元格,例如位于 $A$65536 的单元格

  4. .End(xlUp)说从那个单元格开始,向上找到前一个非空单元格。(如果单元格 $A$65536 非空,它会做一些不同的事情,但我会掩盖它。)

  5. .Row然后找到该非空单元格的行号。为了便于说明,让我们假设这是单元格 $A$5201,因此.Cells(.Rows.Count, "A").End(xlUp).Row将返回5201

  6. "A1:A" & 5201"A1:A""5201"(从数字到字符串的隐式类型转换)连接以生成字符串"A1:A5201"

  7. Range("A1:A5201").ClearContents将清除单元格的内容 A1:A5201