复制单元格内容并将其多次粘贴到另一个工作表

时间:2015-01-24 14:48:20

标签: excel vba excel-vba

我正在尝试将值从两个特定单元格复制到另一个表单中的特定单元格。 问题是我在第一张纸上有很多单元格,其中一些是空的。粘贴总是99次,只是范围变化。是否存在使一切更容易的循环?

这是我的尝试

Sub copytry()

Worksheets("sheetI").Range("I17:J17").Copy _
Destination:=Worksheets("sheetII").Range("F1352:F1451")
Worksheets("sheetI").Range("I18:J18").Copy _
Destination:=Worksheets("sheetII").Range("F1452:F151")

End Sub 

1 个答案:

答案 0 :(得分:0)

练习使用它,

   Sub copytry()
    Dim ws As Worksheet
    Dim sh As Worksheet, lstrw As Long
    Dim Rws As Long, Rng As Range, c As Range

    Set ws = Worksheets("sheetI")
    Set sh = Worksheets("sheetII")

    With ws

        Rws = .Cells(Rows.Count, "I").End(xlUp).Row
        Set Rng = .Range(.Cells(17, "I"), .Cells(Rws, "I"))
        For Each c In Rng.Cells
            If c = "" Then c = "."    'Added to Code
            lstrw = sh.Cells(Rows.Count, "A").End(xlUp).Row + 1
            c.Range("A1:B1").Copy Destination:=sh.Range(sh.Cells(lstrw, 1), sh.Cells(lstrw + 99, 1))

        Next c

    End With

End Sub