选择不连续的单元格并粘贴到下一个空白行的特定列

时间:2018-05-15 12:04:36

标签: excel vba excel-vba

我需要在工作表中选择不连续的单元格并将其粘贴到另一个工作表的下一个空行上的特定列。下面的代码可以复制非连续的单元格并粘贴到所需工作表上的特定单元格,但我无法适应将其复制到下一个空白行。

Sub Copycell()

    Dim rng1 As Range
    Set rng1 = Range("B2,B4,B6")

    Dim rng2 As Range
    Set rng2 = Sheets("list").Range("A2,B2,D2")

    Dim i As Long
    For Each cel In rng2
        cel.Value = rng1.Cells(i + 1)
        i = i + 1
        Next

End Sub

1 个答案:

答案 0 :(得分:0)

Sub Copycell()
    Dim Lastrow as long
    lastrow =  Sheets("list").Cells(sheets("list").rows.count,1).End(xlup).row
    Dim rng1 As Range
    Set rng1 = Range("B2,B4,B6")

    Dim rng2 As Range
    Set rng2 = Sheets("list").Range("A" & lastrow & ",B" & lastrow & ",D" & lastrow)

    Dim i As Long
    For Each cel In rng2
        cel.Value = rng1.Cells(i + 1)
        i = i + 1
        Next

End Sub