插入新行並使用公式自動填充

插入新行並使用公式自動填充

如果滿足特定條件,我嘗試插入新行。我想將公式從最後一行拖曳到插入的行,但這樣做時出現錯誤。下面是我的程式碼:

  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 向下偏移 1 以複製另一行。

 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

希望這對您有用,祝您好運!

相關內容