从工作表1复制粘贴到工作表2

我是一个noob在这个,所以任何帮助表示赞赏。 需要编写一个脚本,根据C的值复制A + B列和粘贴到sheet2的脚本。请参阅下面的示例。 这会为我们节省很多时间。 任何帮助将不胜感激。

工作表Sheet1

ABC 1111 aaaa 3 2222 bbbb 4 3333 cccc 2 

macros后

Sheet2中

 ABC 1111 aaaa 3 1111 aaaa 3 1111 aaaa 3 2222 bbbb 4 2222 bbbb 4 2222 bbbb 4 2222 bbbb 4 3333 cccc 2 3333 cccc 2 

 Sub MakeNewTable() Dim rCell As Range Dim i As Long Dim rNext As Range 'loop through the cells in column A of the source sheet For Each rCell In Sheet1.Range("A1:A3") 'loop as many times as the value in column C of the source sheet For i = 1 To rCell.Offset(0, 2).Value 'find the next empty cell to write to in the dest sheet Set rNext = Sheet2.Cells(Sheet2.Rows.Count, 1).End(xlUp).Offset(1, 0) 'copy A and B from source to the dest sheet rCell.Resize(1, 2).Copy rNext.Resize(1, 2) Next i Next rCell End Sub 

您需要更改图纸参考以符合您的情况。 上面使用的图纸参考是代号参考。 如果你在VBE(Control + R)中打开Project Explorer,并展开你的投影,你会看到像'Sheet1(MyTablName)'这样的对象。 Sheet1部分称为代号,并且在重命名工作表时不会更改。 如果你想使用标签名称,你会使用类似的东西

 ThisWorkbook.Worksheets("MyTabName").Range("A1:A3") 

祝你好运。