【问题标题】:Loop through range and copy cell contents to another worksheet offsetting each time循环遍历范围并将单元格内容复制到另一个工作表,每次偏移
【发布时间】:2014-06-13 01:18:36
【问题描述】:

没有找到解决此问题的方法。使用循环。

主要概念是我的宏从一列单元格中获取值(一次一个),并将它们粘贴到另一个工作表中的另一列,除非每次粘贴的值比上次粘贴的值低 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) How to avoid using Select/Active statements 2) How to determine last used row/column 3) 每次粘贴值时,确定最后一行并添加 7
  • 你想要最后的素材在目的地的顶部还是底部??
  • 在顶部。单元格内容需要按降序粘贴。每个单元之间有 7 个单元格的间隙。现在我写单元格内容的方式每次都写入同一个单元格。我需要它每次在下面写 7 行,直到循环完成。

标签: excel vba loops copy paste


【解决方案1】:

您可以使用.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

【讨论】:

  • 是的,谢谢@Cor_Blimey。这很完美。感谢您的努力。
猜你喜欢
  • 2013-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-07
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
相关资源
最近更新 更多