vba vba增加范围的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6712665/
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-08 11:38:40  来源:igfitidea点击:

vba incrementing a range's row

vbaexcel-vbaexcel

提问by jz3

this should be a really quick question.

这应该是一个非常快速的问题。

Is there any way to increment the value of a cell.Row? For example, can I say something like usedCell.Row = usedCell.Row + 1?

有没有办法增加 cell.Row 的值?例如,我可以说 usedCell.Row = usedCell.Row + 1 之类的吗?

That particular format doesn't work, but is there another way to increase the row by 1?

该特定格式不起作用,但是否有另一种方法可以将行增加 1?

回答by Excellll

I believe cell.Offset(1,0)is what you are looking for.

我相信cell.Offset(1,0)这就是你正在寻找的。

回答by barrowc

The Rowand Columnproperties of a cell (i.e. Range) are read-only so you can't increment them directly.

单元格的RowColumn属性(即Range)是只读的,因此您不能直接增加它们。

If you want to move through the cells in column A then iDevlop's answer works fine. An alternative method is to use the Cellsmethod of the Worksheetobject. Example code to write the word "hello" into every cell in column A from row 1 to 100:

如果您想浏览 A 列中的单元格,那么 iDevlop 的答案可以正常工作。另一种方法是使用对象的Cells方法Worksheet。将单词“hello”写入 A 列第 1 行到第 100 行的每个单元格的示例代码:

Dim lRow As Long

For lRow = 1 To 100
    Worksheets("Sheet1").Cells(lRow, 1).Value = "hello"
Next lRow

As you can see in the example, the Cellsmethod takes the row number as the first parameter and the column number as the second parameter.

正如您在示例中看到的,该Cells方法将行号作为第一个参数,将列号作为第二个参数。

For the simple case of dealing with cells in the same column, you could also use the Rangeproperty of the Worksheetobject and construct the actual address - e.g. A39 - each time:

对于处理同一列中的单元格的简单情况,您还可以使用对象的Range属性Worksheet并每次构造实际地址 - 例如 A39:

Dim lRow As Long

For lRow = 1 To 100
    Worksheets("Sheet1").Range("A" & lRow).Value = "hello"
Next lRow

回答by Patrick Honorez

Have you considered using

你有没有考虑使用

Dim c as Range
For Each c in Range("a:a")
    ...
Next c

?

?

回答by Ben Fedorko

You may be looking for

您可能正在寻找

Set rng = rng.Resize(RowSize, ColumnSize)

设置 rng = rng.Resize(RowSize, ColumnSize)

In your case specifically

在你的情况下

Dim rng_usedCells As Range

Set rng_usedCells = ActiveSheet.Range("A1:B10")

ActiveSheet.Range("D2").Value = rng_usedCells.Rows.Count
ActiveSheet.Range("E2").Value = rng_usedCells.Columns.Count

Set rng_usedCells = rng_usedCells.Resize(3, 4)

ActiveSheet.Range("D3").Value = rng_usedCells.Rows.Count
ActiveSheet.Range("E3").Value = rng_usedCells.Columns.Count

rng.Resize

rng.调整大小