2013-09-30 26 views
0

我已经记录了一个在我想使用的表中创建列的宏,现在我想在每一行中输入内容。我发现这段代码,将要求输入的行数和显示许多空行:如何编辑Excel-VBA中的行 - 测试数据生成

Dim j As Long, r As Range 
j = InputBox("type the number of rows to be insered") 
Set r = Range("A2") 
Do 
Range(r.Offset(1, 0), r.Offset(j, 0)).EntireRow.Insert 
Set r = Cells(r.Row + j + 1, 1) 
MsgBox r.Address 
If r.Offset(1, 0) = "" Then Exit Do 
Loop 

我想知道究竟怎么了(其中在环?)我插入的东西在这些行基于我拥有的专栏?哪个功能可以让我知道?

+0

在'Set r = Cells(r.Row + j + 1,1)'段之前假设你可以输入你想要的任何引用并在那里做东西。你到底想做什么?你可以在任何地方通过参考“插入东西”。 –

+0

就像我有一个列ID,名字,姓氏,出生日期。就像列名一样,我想要从名称数组中选择一个随机名称? ...出生日期(日期范围内的随机日期),id为随机不可重复的数字。 – Thatdude1

+0

@JoeLaviano究竟如何将它与每一列相关联? – Thatdude1

回答

1

我认为你在寻找这样的事情

Sub tgr() 

    Dim InsertCount As Long 
    Dim rIndex As Long 

    InsertCount = Int(Application.InputBox("Type the number of rows to be inserted", "Insert Rows", Type:=1)) 
    If InsertCount <= 0 Then Exit Sub 'Pressed cancel, or entered a negative number 

    For rIndex = Cells(Rows.Count, "A").End(xlUp).Row To 3 Step -1 
     Rows(rIndex).Resize(InsertCount).Insert 
     With Cells(rIndex, "A").Resize(InsertCount) 
      MsgBox .Address 

      ''''''''''''''''''''''''''''''''''''''''''''''''''' 
      '             ' 
      ' Code goes here to insert data into column A ' 
      '             ' 
      ''''''''''''''''''''''''''''''''''''''''''''''''''' 

     End With 
    Next rIndex 

End Sub 

你可以用不同的列字母加With Cells(rIndex, ...附加节将数据插入到这些列。

+0

因此,如果我想在“Code goes here”中将列A中的所有列都设为“Bob”,那么我会执行类似“Cells(rIndex,2).Value =”BOB“'? – Thatdude1

+0

如果您希望列A的所有内容都是“Bob”(并且只在新插入的行中),那么您将用'.Value =“Bob”替换“Code goes here”评论框。 – tigeravatar