简体   繁体   English

VBA复制并粘贴到下面的第+1行

[英]VBA Copy and Paste to Row +1 below

I'm trying to write some code that will copy and paste data in cells A3:A6 to A8:A11 and then when run again it will paste it the row +1 beneath, so the next time it's run the data in A8:A11 will be copy and pasted into A13:A16 and the next time after that it's run it will paste the data in A13:16 to A18:21 and so on. 我正在尝试编写一些代码,将数据复制并粘贴到单元格A3:A6到A8:A11中,然后再次运行时,它将粘贴到下面的+1行中,因此下一次在A8:A11中运行数据将被复制并粘贴到A13:A16中,并且下次运行后,它将把A13:16中的数据粘贴到A18:21中,依此类推。

The below is what I've tried to come up with but I might be quite a way off, any guidance will be appreciated: 以下是我尝试提出的内容,但我可能还有一段距离,任何指导将不胜感激:

Sub RollFile()

Dim UsdRows As Long

UsdRows = Cells(Rows.Count, 3).End(xlToUp).Row
With Range(Cells(1, UsdRows), Cells(UsdRows, 1))
  .Copy .Offset(, 1)
  .Value = .Value
  .Offset(-1, 1)(1).Select
End With

End Sub

Many thanks 非常感谢

I suggest the following: 我建议以下内容:

Option Explicit

Public Sub RollFile()
    Const RowsToCopy As Long = 4 'amount of rows that should be copied

    Dim LastCell As Range
    Set LastCell = Cells(Rows.Count, "A").End(xlUp) 'last cell in col A

    With LastCell.Offset(RowOffset:=-RowsToCopy + 1).Resize(RowSize:=RowsToCopy) '= last 4 cells (4 = RowsToCopy)
        .Copy LastCell.Offset(RowOffset:=2)
        .Value = .Value 'not needed I think
    End With
End Sub

It looks for the last used cell in column A. Then selects the previous 4 cells from there and copies that 2 rows below. 它在A列中查找最后使用的单元格。然后从那里选择前4个单元格,并在下面复制2行。

Note that I think .Value = .Value is not needed at all because that only makes sense if formulas were copied that need to be transformed into values. 请注意,我认为根本不需要.Value = .Value ,因为这仅在复制了需要转换为值的公式时才有意义。

you could try this 你可以试试这个

Sub RollFile()

    With Cells(Rows.Count, 1).End(xlUp) ' reference column A last not empty cell
        With Range(.End(xlUp), .Cells) ' reference the range from referenced cell up to last adjacent one
            .Offset(.Rows.Count + 1).Value = .Value ' copy referenced range values to a range two row below its end
        End With
    End With

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM