我想将一个特殊格式化/公式化的行(来自模板工作表)粘贴到主工作表上正在修改的同一行上。到目前为止,这是我所拥有的,但出现运行时错误 1004:
"PasteSpecial method of Ranged class failed"
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("A5:A10000")
'the template of a very long formatted row with formulats
Set TemplateRow = ActiveWorkbook.Worksheets("Templates").Range("A1:BB1")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
TemplateRow.Copy
Range(Target.Address).PasteSpecial Paste:=8
Range(Target.Address).PasteSpecial Paste:=-4104
Application.CutCopyMode = False
Else
Range(Target.Address).EntireRow.Delete
End If
End Sub
最佳答案
你能帮我试试这个吗? (未经测试)
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range, TemplateRow As Range
If Target.Cells.CountLarge > 1 Then Exit Sub
On Error GoTo Whoa
Application.EnableEvents = False
Set KeyCells = Range("A5:A10000")
Set TemplateRow = Worksheets("Templates").Range("A1:BB1")
If Not Intersect(Target, KeyCells) Is Nothing Then
TemplateRow.Copy
Target.PasteSpecial Paste:=8
DoEvents
TemplateRow.Copy '<~~ Insurance against clipboard getting cleared
Target.PasteSpecial Paste:=-4104
End If
Letscontinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
关于Excel VBA : How do I PasteSpecial into same row of Cell location after Cell is changed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21490887/