在EXCL表中用VBA插入的一行,自动复制上行格式和公式

在EXCL表中用VBA插入的一行,自动复制上行格式和公式.详细些.

输入一行会触发:Change事件
可以在这个该事件中写入代码就可以了。
(北京 黄吕娜)
追问:
能帮助我写个完整的代码吗?

追答:
Private Sub Worksheet_Change(ByVal Target As Range)
a = Target.Row()
b = Target.Column()
If a <> 1 Then
If Cells(a, b) = "" And Cells(a - 1, b) <> "" Then
Rows(a - 1 & ":" & a - 1).Copy
Cells(a, b).Select
ActiveSheet.Paste
c = Cells(a, b)
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = c
Cells(a, b).Select
End If
End If
End Sub
追问:
您好!我运行代码发现,连同上一行的数据也复制了,我想只复制公式即可,不用复制没有公式的数据,应该如何修改代码?非常感谢您!

追答:
用下面的代码吧,我重写过了!
Private Sub Worksheet_Change(ByVal Target As Range)
a = Target.Row()
b = Target.Column()
Application.ScreenUpdating = False
If a <> 1 Then
If Cells(a, b) = "" And Cells(a - 1, b) <> "" Then
For x = 1 To 256
If Cells(a - 1, x).HasFormula = True Then
Cells(a - 1, x).Copy
Cells(a, x).Select
ActiveSheet.Paste
Application.CutCopyMode = False
End If
Next x
Cells(a, b).Select
End If
End If
Application.ScreenUpdating = True
End Sub追问

不懂VBA不知如何弄,最好传个做好的表给嘛,

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-11-11
把你的需求稍微调整了下:选择某一行,然后在其下面插入一行,其格式和公式复制上一行的。如果是要做习题的话,我再重新给你处理下,但这个是比较容易的处理!

运行: 选中某一行,然后^+P

Sub Macro1()
'
' Macro1 Macro
'
' 快捷键: Ctrl+p
'
Dim r As Integer
r = Selection.Row
Selection.Copy
Rows(r + 1).Select
Selection.Insert Shift:=xlDown
End Sub本回答被网友采纳
相似回答