【问题标题】:PasteSpecial method of Range class failed when trying to paste into an added ListRowRange 类的 PasteSpecial 方法在尝试粘贴到添加的 ListRow 时失败
【发布时间】:2021-02-09 07:39:12
【问题描述】:

我想将活动工作表中的数据 A1:A61 复制/粘贴到包含格式化表格的“客户信息”工作表的第 2 列“B”到第 62 列“BJ”中。我在“ClientInfoTable”的第 68 列“BP”中搜索客户姓名和电话。如果找到,那就是我要更新的行,否则添加一个新行并粘贴到其中。 PasteSpecial 在更新 FOUND 记录时工作正常,但仅在尝试将 PasteSpecial 插入添加的行时失败。错误:Range 类的 PasteSpecial 方法失败。

Sub SaveToMaster(clientfirstname as String, clientlastname as String, clientphone as String)

    Dim addedrow As ListRow
    Dim tbl As ListObject
    Dim clientnamephone As String
    Dim foundcell As Range
    Dim updaterow As Long

    clientnamephone = clientfirstname & " " & clientlastname & " " & clientphone
    
    Range("A1:A61").Copy
    Worksheets("Client Info").Activate
    
    Set tbl = ActiveSheet.ListObjects("ClientInfoTable")

    Set foundcell = tbl.DataBodyRange.Columns(68).Find(What:=clientnamephone, LookIn:=xlValues, LookAt:=xlWhole)

    If foundcell Is Nothing Then
        Set addedrow = tbl.ListRows.ADD
        updaterow = addedrow.Index
    Else
        updaterow = tbl.ListRows(foundcell.Row - tbl.HeaderRowRange.Row).Index
    End If
    
    tbl.DataBodyRange(updaterow, 2).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True

End Sub

【问题讨论】:

    标签: excel


    【解决方案1】:

    我明白了原因:当您添加 ListRow 时,它显然会清除复制的范围。我通过返回源工作簿(添加列表行之后)并复制源范围来纠正它。

    【讨论】:

      猜你喜欢
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 2019-04-13
      • 1970-01-01
      相关资源
      最近更新 更多