[英]Insert row with formulas below active cell
I use this code to add a row below a selected cell.我使用此代码在所选单元格下方添加一行。 Is there any way to alter it so that it pastes formulas in columns E, G, and H, (for example: =IF(G4="","",G4*73)) but clears the contents of cells in Columns AD, F?
有什么方法可以改变它,使它在 E、G 和 H 列中粘贴公式(例如:=IF(G4="","",G4*73))但清除 AD 列中单元格的内容, 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
Or maybe just insert the row below, but just copy the formulas down?或者也许只是插入下面的行,但只是将公式复制下来?
Frankly, I would have thought that there is a variation of PasteSpecial that copies formulas but not values, but if there is I didn't find it.坦率地说,我原以为 PasteSpecial 有一个变体,它复制公式而不是值,但如果有,我没有找到它。 The code below does the job, however.
但是,下面的代码可以完成这项工作。 Please try it
请尝试一下
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
May be easier to do without loops like that没有这样的循环可能更容易做到
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.