input新行并从上面的单元格复制公式
我正在尝试创build一个Excelmacros来执行以下操作:
-
在文档的末尾input新行
-
从上面的单元格复制公式
到目前为止,我有这样的:
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
您可以简单地将之前的行复制/插入到新行中
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