循环遍历范围并将单元格内容复制到每次偏移的另一个工作表

时间:2014-04-27 15:16:26

标签: excel vba loops copy paste

没有任何运气找到解决这个问题的方法。使用循环。

主要概念是我的宏从一列单元格中获取值(一次一个)并将它们粘贴到另一个工作表中的另一列,除非每次粘贴的值是从最后一个粘贴下来的7个单元格。 - 感谢您考虑我的要求。

Sub LoopData()

    Dim X As Integer
    NumRows = Range("A1", Range("A2").End(xlDown)).Rows.Count

    Range("A1").Select
    For X = 1 To NumRows

    Selection.Copy _
        Destination:=Worksheets("Sheet1").Range("A1")

    'not sure for what code to put here to move the copied contents down 7 cells each time    

    ActiveCell.Offset(1, 0).Select
    Next 
End Sub

1 个答案:

答案 0 :(得分:2)

您可以使用.Offset执行某些操作,例如:

Sub blah()
    Dim inRng As Range, outCell As Range, inCell As Range

    Set inRng = Selection 'change
    Set outCell = Range("B1") 'change to be first cell of output range

    Application.ScreenUpdating = False

    For Each inCell In inRng
        inCell.Copy outCell 'if you want Values only (instead of Copy) then use outCell.Value = inCell.Value
        Set outCell = outCell.offset(7, 0)
    Next inCell

    Application.ScreenUpdating = True

End Sub