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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23325080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:54:27  来源:igfitidea点击:

Loop through range and copy cell contents to another worksheet offsetting each time

excelvbaloopscopypaste

提问by user3578485

Not having any luck finding a solution to this problem. Working with loops.

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

The main concept is that my macro takes values from one column of cells (one at a time) and pastes them to another column in another worksheet except each time the values getting pasted are 7 cells down from the last paste. - Thanks for considering my request.

主要概念是我的宏从一列单元格(一次一个)中获取值并将它们粘贴到另一个工作表中的另一列,除非每次粘贴的值比上次粘贴低 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

回答by Cor_Blimey

You can do something using .Offsetlike:

您可以使用以下方法执行某些操作.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