Excel 在活动单元格下方插入带有公式的行

Excel 在活动单元格下方插入带有公式的行,excel,vba,insert,row,Excel,Vba,Insert,Row,我使用此代码在选定单元格下方添加一行。是否有任何方法对其进行修改,使其将公式粘贴到列E、G和H中(例如:=IF(G4=“”,“”,G4*73)),但清除列A-D、F中的单元格内容 Sub BlankLine_copy() lrow = Selection.Row() Rows(lrow).Select Selection.Copy Rows(lrow + 1).Select Selection.Insert Shift:=xlDown Application.CutCopyMode = True

我使用此代码在选定单元格下方添加一行。是否有任何方法对其进行修改,使其将公式粘贴到列E、G和H中(例如:=IF(G4=“”,“”,G4*73)),但清除列A-D、F中的单元格内容

Sub BlankLine_copy()
lrow = Selection.Row()
Rows(lrow).Select
Selection.Copy
Rows(lrow + 1).Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = True
'Selection.ClearContents
End Sub

或者只是在下面插入一行,只是把公式复制下来?

坦率地说,我会认为有一种特殊的粘贴方式,它复制公式而不是值,但如果有,我没有找到它。然而,下面的代码完成了这项工作。请试一试

Sub BlankLine_copy()

    Dim R As Long
    Dim C As Long

    R = Selection.Row
    With Rows(R)
        .Copy
        .Insert Shift:=xlDown
    End With
    Application.CutCopyMode = False

    R = R + 1
    For C = Cells(R, Columns.Count).End(xlToLeft).Column To 1 Step -1
        With Cells(R, C)
            If Not .HasFormula Then .ClearContents
        End With
    Next C
    Cells(R, 1).Select
End Sub

没有这样的循环可能更容易做到

Sub Test()
Dim r As Long

r = Selection.Row
With Rows(r)
    .Copy
    .Insert Shift:=xlDown
End With
Application.CutCopyMode = False

Rows(r + 1).SpecialCells(xlCellTypeConstants).ClearContents
End Sub

效果很好!谢谢你的时间和努力!