循环遍历行以复制和粘贴特殊

时间:2012-05-02 07:49:23

标签: excel vba for-loop

电子表格包含以下数据:

      A                B    C   D    E
2  20120425 09:55:00  101  99  102  100.50   
3  20120425 09:55:00  101  102  98  101.50

我想在同一张纸上执行此操作:

      H       
2  101    
3  99   
4  102  
5  100.50

复制范围B2:E2然后选择H2并粘贴特殊(它将粘贴在H2,H3,H4,H5上)。然后重复B3的任务:E3复制范围并在H6上粘贴特殊。

我录制了宏,但任务必须重复近5000行。

录制的代码

Sub Macro9()
'
' Macro9 Macro
'

'
    Range("D1:G1").Select
    Selection.Copy
    Range("L1:L4").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
    Range("D2:G2").Select
    Selection.Copy
    Range("L5:L8").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
End Sub

1 个答案:

答案 0 :(得分:1)

以某种方式找到答案。

Sub alex4()

Dim k As Integer, i As Integer, lngRows As Integer    

lngRows = Range("A1").CurrentRegion.Rows.Count    

For k = 1 To (lngRows * 4)         

    For i = 1 To lngRows    

        k = k + 4   

        Range(Cells(i, 4), Cells(i, 7)).Select    

        Selection.Copy    

        Range(Cells(k, 12), Cells(k + 3, 12)).Select    

        Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
          False, Transpose:=True    

    Next i    

Next k      

End Sub