VBA:结合并转移到新表

时间:2014-11-24 15:31:25

标签: vba copy move paste

使用VBA,我该如何解决以下问题:

我需要合并column Arow 1,2,3,...,lastrow)&和column B row a,b,csheet1column A以及Column B sheet2,如下所示:

1 a

1 b

1 c

2 a

2 b

2 c

3 a

3 b

3 c

...

lastrow a

lastrow b

lastrow c

我希望这是有道理的。

1 个答案:

答案 0 :(得分:0)

这是一个可以满足您需求的宏。有关在此处运行宏的更多信息:http://office.microsoft.com/en-us/excel-help/run-a-macro-HP010342865.aspx

Sub CopyExtra()

Dim LastRow As Long
Dim CurRow As Long
Dim DestRow As Long

LastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row

For CurRow = 1 To LastRow
    DestRow = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row + 1
    With Sheets("Sheet2")
        .Range("A" & DestRow & ":A" & DestRow + 2).Value = Sheets("Sheet1").Range("A" & CurRow).Value
        Sheets("Sheet1").Range("B1:B3").Copy
        .Range("B" & DestRow & ":B" & DestRow + 2).PasteSpecial xlPasteValues
    End With
Next CurRow
End Sub
相关问题