3

我正在尝试创建一个执行以下操作的 Excel 宏:

  1. 在文档末尾输入新行

  2. 从上面的单元格中复制公式

到目前为止,我有这个:

    Sub New_Delta()

    ' Go to last cell
    Range("A4").Select
    Selection.End(xlDown).Select
    LastCell = [A65536].End(xlUp).Offset(-1, 0).Address
    Range(LastCell).Select

    ' Enter new line
    Selection.EntireRow.Insert Shift:=xlUp, CopyOrigin:=xlFormatFromLeftOrAbove

    ' Copy formula from cell above
    Dim oCell As Range
        For Each oCell In Selection
            If (oCell.Value = "") Then
            oCell.Offset(-1, 0).Copy Destination:=oCell
            End If
        Next oCell

End Sub

这会复制第一个单元格“A”的公式,但不会复制以下单元格的公式

我想做类似的事情Selection.Offset(0, 1).Select,然后迭代到“K”(最好没有“G”和“H”)

但我被困住了,真的可以使用一些帮助。

编辑:我想要这样的东西(非工作伪代码)

    ' Copy formula from cell above
Dim oCell As Range
        While (oCell.Offset(-1, 0).Value != "") ' If the cell above is not empty
        oCell.Offset(-1, 0).Copy Destination:=oCell ' Copy the formula from the cell above
        Selection.Offset(0, 1).Select ' Move one cell to the right
4

1 回答 1

8

您可以简单地将之前的行复制/插入到新行中

Sub New_Delta()

  ' Go to last cell
  Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select

  ' Copy formula from cell above
  Rows(Selection.Row - 1).Copy
  Rows(Selection.Row).Insert Shift:=xlDown

End Sub
于 2013-10-09T11:40:34.657 回答