插入新行并使用公式自动填充

插入新行并使用公式自动填充

如果满足特定条件,我尝试插入新行。我想将公式从最后一行拖到插入的行,但这样做时出现错误。以下是我的代码:

  For i = 1 To diff
    MsgBox ("Difference is " & diff)

    With Sheet5.Rows(ModelLastRow.Row + 1)
       .Insert Shift:=xlDown
    End With

    insertRowRange = "C" & CStr((ModelLastRow.Row))

    pasteRowRange = "C" & CStr((ModelLastRow.Row + 1))
    Range("insertRowRange").AutoFill Destination:=Range("pasteRowRange")
  Next

答案1

看起来您只是试图将单个单元格向下复制一行。我假设“ModelLastRow”是您要向下复制的单个单元格。在循环的每一步中,我都会将 ModelLastRow 向下偏移一行以复制另一行。

 Dim ModelNextRow As Range
 MsgBox ("Difference is " & diff)
 For i = 1 To diff

     Set ModelNextRow = Union(ModelLastRow, ModelLastRow.Offset(1, 0))
     ModelLastRow.AutoFill Destination:=ModelNextRow, Type:=xlFillDefault

     Set ModelLastRow = ModelLastRow.Offset(1, 0)
   Next

 End Sub

希望这对你有用,祝你好运!

相关内容