Excel VBA : How do I PasteSpecial into same row of Cell location after Cell is changed?

标签 excel vba

我想将一个特殊格式化/公式化的行(来自模板工作表)粘贴到主工作表上正在修改的同一行上。到目前为止,这是我所拥有的,但出现运行时错误 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/

相关文章:

excel - 使用条件格式和图标集来比较单元格值差异

VBA 试图指定一个新创建的工作簿而不是事件工作簿(将一些数据粘贴到新工作簿中)

excel - 如何计算 Excel 中两个 ISO 8601 日期时间之间的秒数?

Excel VBA代码将工作表向右或向左移动一个

vba - MSXML2.DOMDocument 加载函数在 VBA 中失败

excel - Excel 2007 UDF:如何添加函数说明,参数帮助?

arrays - 对数组的特定列进行计算

vba - Excel-VBA 中的求和

vba - 迭代 Excel VBA 或 VSTO 2005 中的所有单元格

c# - 自动将思维导图转换为另一个应用程序(在 MS Project 中)