解决listobject paste special - 运行时间错误1004

Solving listobject paste special - Run time error 1004

我想将所有数据从一个 table 移动到另外 4 个 table。我在使用下面的代码时遇到特殊粘贴错误。代码很长,因此在下面发布了相关代码段。

Set tbl2 = ws1.ListObjects("Table2")
Set tbl3 = ws2.ListObjects("Table3")
Set tbl4 = ws3.ListObjects("Table4")
Set tbl5 = ws4.ListObjects("Table5")

For i = 1 To tbl1.ListRows.Count
    tbl1.ListRows(i).Range.Copy

    tbl2.ListRows.Add 1, True
    tbl2.ListRows(1).Range.PasteSpecial xlPasteFormats
    tbl2.ListRows(1).Range.Value = tbl1.ListRows(i).Range.Value

    tbl3.ListRows.Add 1, True
    tbl3.ListRows(1).Range.PasteSpecial xlPasteFormats
    tbl3.ListRows(1).Range.Value = tbl1.ListRows(i).Range.Value


    tbl4.ListRows.Add 1, True
    tbl4.ListRows(1).Range.PasteSpecial xlPasteFormats
    tbl4.ListRows(1).Range.Value = tbl1.ListRows(i).Range.Value

    tbl5.ListRows.Add 1, True
    tbl5.ListRows(1).Range.PasteSpecial xlPasteFormats
    tbl5.ListRows(1).Range.Value = tbl1.ListRows(i).Range.Value

Next i

"Run time error 1004": 范围 class 的特殊粘贴失败

第一次粘贴特殊行触发此错误

关于如何解决这个问题有什么想法吗?我已经在 stacked 上搜索了一段时间,但还没有找到解决方案。

谢谢!

您似乎只是将 tbl1 的内容附加到一堆其他 table 中。

不涉及剪贴板,而是将源 DataBodyRange 复制到二维变体数组:

Dim content As Variant
content = tbl1.DataBodyRange.Value

然后向您的目的地添加一个新行:

tbl2.ListRows.Add

并将二维数组转储到该位置:

tbl2.ListRows(tbl2.ListRows.Count).Range.Resize(UBound(content, 1)).Value = content

为每个目的地冲洗并重复 table...应该很快。