我是VBA的新手,试图自学。我想在一个表中复制行并将它们粘贴到另一个表上的新表中。首先,我想将行复制到原始表的底部,这个脚本完美地工作: '
Dim a As Range, b As Range
'Set a = Selection
'For Each b In a.Rows
'Set LastRow = ActiveSheet.ListObjects("Table1").ListRows.Add
'b.Copy
'LastRow.Range.PasteSpecial xlPasteAll
'Next
但是当我试图简单地将目的地更改为另一张表和表时,我得到了运行时错误9,下标超出范围。
Dim a As Range, b As Range
Set a = Selection
For Each b In a.Rows
b.Copy
Set LastRow = Worksheets _
("Sheet2").ListObjects("Table2").ListRows.Add
LastRow.Range.PasteSpecial xlPasteAll
Next
我已经尝试了很多东西来解决这个问题,而我似乎无法做到这一点。帮助赞赏!
答案 0 :(得分:0)
它对我有用。你对Lastrow的声明是什么? 它复制我在'表1和#39;中选择的行。来自Sheet1并将其附加到'表1'在Sheet2中。
Dim a As Range, b As Range
Dim Lastrow As Variant
Set a = Selection
For Each b In a.Rows
b.Copy
Set Lastrow = Worksheets("Sheet2").ListObjects("Table2").ListRows.Add
Lastrow.Range.PasteSpecial xlPasteAll
Next