【发布时间】: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