如何在 Excel 中使用 VBA 自动填充“x”行数

act*_*ife 3 excel parsing vba copy

我正在做的总体思路是从用户窗体接收用户输入,将该数据添加到单行,然后我想在该原始行正下方复制该行“x”次。这种情况下的“x”行数也由用户输入的值决定。

例如,假设用户填写 UserForm 并且他们选择的“x”是 5。我的 VBA 代码将获取他们输入的信息,将其放在第 2 行,然后基本上复制/粘贴该行到接下来的 4 行排下来。然后最终结果看起来像这样(除了 C 列中的每个单元格都将是“44”,我不确定为什么该列决定增加 1,但这是我需要帮助修复的另一件事):

在此处输入图片说明

我假设我可以编写 VBA 代码来复制和粘贴行 'x' 次数,但我认为自动填充会更容易,因为每一行的值都相同。我只是不知道如何指定一个范围,该范围将根据用户输入的内容而变化。这是我为特定范围自动填充的代码,我不确定如何将其更改为不同的范围


numOfLines = (LastL - FirstL) + 1    'don't worry about how I get this, it's a number that I know is correct

   With Sheets("QuoteCSV")
    
        Sheets("QuoteCSV").Select
        
        ' Get the current row
        Dim curRow As Long
        If .Range("B1") = "" Then
            curRow = 1
        Else
            curRow = .Range("B" & .Rows.Count).End(xlUp).Row + 1
        End If
        
        ' Add items to the first row (row 2)
        .Cells(curRow, 1) = fson
        .Cells(curRow, 2) = fdiv
        .Cells(curRow, 3) = fcnum
        .Cells(curRow, 4) = fcponum
        .Cells(curRow, 5) = frdate
        .Cells(curRow, 6) = cname
        .Cells(curRow, 7) = add1
        .Cells(curRow, 8) = add2
        .Cells(curRow, 9) = city
        .Cells(curRow, 10) = state
        .Cells(curRow, 11) = zip
        
        
        'Now I want to take that row I just made and autofill it down 'numOfLines' rows

        Range("A2:K2").Select
        Application.CutCopyMode = False
        Selection.AutoFill Destination:=Range("A2:K6"), Type:=xlFillDefault 
        'need to change this line to use numOfRows somehow instead of hard-coded range
        
        
    End With
    ```

Any help is appreciated!
Run Code Online (Sandbox Code Playgroud)

Big*_*Ben 6

也许不是使用AutoFill,而是使用Resize

With .Range("A2:K2")
    .Resize(numOfRows).Value = .Value
End With
Run Code Online (Sandbox Code Playgroud)