0

我已经录制了一个宏来在我想使用的表中创建列,现在我想在每一行中输入内容。我发现这段代码会询问输入的行数并显示许多空白行:

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  

我想知道如何根据我拥有的列将内容插入到这些行中(在循环中的哪个位置?)?哪个功能可以让我这样做?

4

1 回答 1

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, ...以将数据插入这些列。

于 2013-09-30T18:24:51.987 回答