I am new to VBA, trying to teach myself. I want to copy rows in one table and paste them into a new table on another sheet. At first, I wanted to copy the rows to the bottom of the original table, and this script worked perfectly: '
我是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
But when I tried to simply change the destination to another sheet and table, I got Runtime Error 9, Subscript Out of Range.
但是,当我试图简单地将目标更改为另一个工作表和表时,我得到了运行时错误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
I've tried a whole bunch of things to fix this and I can't seem to get it right. Help appreciated!
我已经尝试了很多东西来解决这个问题,我似乎无法做到这一点。帮助赞赏!
1 个解决方案
#1
0
It works for me. What is your declaration for Lastrow? It copies the rows I selected in 'Table1' from Sheet1 and appends it to the 'Table1' In Sheet2.
这个对我有用。你对Lastrow的声明是什么?它从Sheet1复制我在'Table1'中选择的行,并将其附加到Sheet2中的'Table1'。
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
#1
0
It works for me. What is your declaration for Lastrow? It copies the rows I selected in 'Table1' from Sheet1 and appends it to the 'Table1' In Sheet2.
这个对我有用。你对Lastrow的声明是什么?它从Sheet1复制我在'Table1'中选择的行,并将其附加到Sheet2中的'Table1'。
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