如何在 Excel VBA 脚本中增加单元格?

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

How do I increment cell in Excel VBA script?

excelvbaexcel-vbaexcel-2007

提问by js0823

I have a data in excel which I want to make a VBA script to copy it into a new worksheet but in a different way. For example, I have this in sheet1 in A1~A3 cells.

我在 excel 中有一个数据,我想制作一个 VBA 脚本来将它复制到一个新的工作表中,但以不同的方式。例如,我在 A1~A3 单元格的 sheet1 中有这个。

Adam(A1)
Sam(A2)
Smith(A3)

I want to use these cells and create the following in another worksheet using refedit control.

我想使用这些单元格并使用 refedit 控件在另一个工作表中创建以下内容。

Adam(A1)
Adam(A2)
Adam(A3)
Adam(A4)
Sam(A5)
Sam(A6)
Sam(A7)
Sam(A8)
Smith(A9)
Smith(A10)
Smith(A11)
Smith(A12)

I have refedit control in place in VBA script, but I'm not sure how to increment cell numbers to make it copy and paste into a new worksheet. I would like to use refedit control so that I can assign any cells and make it copy and repeat itself. How do I do this in VBA script?

我在 VBA 脚本中设置了 refedit 控件,但我不确定如何增加单元格编号以使其复制并粘贴到新工作表中。我想使用 refedit 控件,以便我可以分配任何单元格并使其复制和重复。如何在 VBA 脚本中执行此操作?

回答by AMissico

Check out the RangeRows, Cells, and Addressproperties. This should help. Your question is too vague for a direct answer.

退房的RangeRowsCellsAddress性质。这应该有帮助。你的问题太含糊,无法直接回答。



(This will get you started.)

(这会让你开始。)

Range.Row Property

Range.Row 属性

http://msdn.microsoft.com/en-us/library/bb221550(office.12).aspx

http://msdn.microsoft.com/en-us/library/bb221550(office.12).aspx

Returns the number of the first row of the first area in the range. Read-only Long.

返回范围内第一个区域的第一行的编号。只读长。

Example

例子

For Each rw In Worksheets("Sheet1").Rows
    If rw.Row Mod 2 = 0 Then
        rw.RowHeight = 4
    End If
Next rw 

回答by systemovich

To increment cells in Excel VBA, you can use the Offset-property of the Range-object, e.g.

要在 Excel VBA 中增加单元格,您可以使用 Range-object 的 Offset-property,例如

ActiveCell.Offset(1, 1).Select

will select the cell one row down and one column to the right of the active cell.

将选择活动单元格右下一行和一列的单元格。

回答by drD

To add to Geoffrey's answer about active cell - it would also require that you activate the sheet you are looking to input your values if it is a different sheet from the one that is currently active. Additionally you would have to activate a cell to use activecell and the activecell offset property. For example

要添加到 Geoffrey 关于活动单元格的答案中 - 如果它与当前活动的工作表不同,则还需要您激活要输入值的工作表。此外,您必须激活一个单元格才能使用 activecell 和 activecell offset 属性。例如

     'Activates the name of the sheet you would like to activate
    Sheets("Sheet2").Activate  

    'Activates cell A1
    Range("A1").Activate

    'Activates cell one row down, one column right
    ActiveCell.Offset(1,1).Select  

   'if current sheet is not activate you just do Sheets("Sheet2").Range("A1").Activate

The offset property of ActiveCell refers to other cells based off of the current active cell.

ActiveCell 的 offset 属性是指基于当前活动单元格的其他单元格。

For example-

例如-

Offset(row,column) -

偏移量(行,列) -

First Argument -Positive values as the first argument refer you to rows below the current active cell and Negative values refer you to rows above the current active cell

第一个参数 - 作为第一个参数的正值表示当前活动单元格下方的行,负值表示当前活动单元格上方的行

Second Argument-Positive values as the second argument refer you to columns right of the current active cell and Negative values refer you to columns left the current active cell

第二个参数 - 作为第二个参数的正值将您指向当前活动单元格右侧的列,负值将您指向当前活动单元格左侧的列