简体   繁体   English

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

[英]How do I increment cell in Excel VBA script?

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. 我在excel中有一个数据,我想制作一个VBA脚本以另一种方式将其复制到新的工作表中。 For example, I have this in sheet1 in A1~A3 cells. 例如,我在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. 我在VBA脚本中拥有refedit控件,但是我不确定如何增加单元格编号以使其复制并粘贴到新工作表中。 I would like to use refedit control so that I can assign any cells and make it copy and repeat itself. 我想使用refedit控件,以便我可以分配任何单元格并使其复制并重复自身。 How do I do this in VBA script? 如何在VBA脚本中执行此操作?

Check out the Range Rows , Cells , and Address properties. 签出“ Range Rows ,“ Cells ”和“ Address属性。 This should help. 这应该有所帮助。 Your question is too vague for a direct answer. 您的问题太含糊,无法直接回答。


(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. 只读Long。

Example

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

To increment cells in Excel VBA, you can use the Offset-property of the Range-object, eg 要在Excel VBA中增加单元格,可以使用Range对象的Offset属性,例如

ActiveCell.Offset(1, 1).Select

will select the cell one row down and one column to the right of the active cell. 将选择活动单元格的下一行和右侧一列的单元格。

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. 要添加到Geoffrey关于活动单元格的答案中,还需要激活您想要输入值的工作表(如果它与当前活动的工作表不同)。 Additionally you would have to activate a cell to use activecell and the activecell offset property. 另外,您必须激活一个单元才能使用activecell和activecell offset属性。 For example 例如

     '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 第二个参数-正值作为第二个参数,将您引向当前活动单元格的右侧,负值将您引向当前活动单元格的左侧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM