我正在尝试创建一个执行以下操作的Excel宏:
在文档末尾输入一个新行
从上面的单元格中复制公式
到目前为止我有这个:
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
Run Code Online (Sandbox Code Playgroud)
这将复制第一个单元格"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
Run Code Online (Sandbox Code Playgroud)
您可以简单地将行复制/插入到新行中
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40841 次 |
| 最近记录: |