VBA Offseting复制范围无效

时间:2017-08-17 03:09:35

标签: excel-vba vba excel

我在其他页面上有相同的代码,它工作正常,但在一个页面上它没有偏离第三行的粘贴,它只做到第一行可以有人帮忙吗?

Set wsInfoFrom = Worksheets("STP1st")
Set wsInfoTo = Worksheets("StowToPrime")
lastrow = wsInfoFrom.Range("B" & wsInfoFrom.Rows.Count).End(xlUp).Row
Set copyRange = wsInfoFrom.Range("B5:B" & lastrow)

wsInfoTo.Range("A1:A9999" & lastrow).ClearContents

    copyRange.SpecialCells(xlCellTypeVisible).Copy wsInfoTo.Range("A" & Rows.Count).End(xlUp).Offset(3, 0)

1 个答案:

答案 0 :(得分:0)

尝试下面稍加修改和更有条理的代码,我认为这就是你要做的事情:

Set wsInfoFrom = Worksheets("STP1st")
Set wsInfoTo = Worksheets("StowToPrime")

With wsInfoFrom
    lastrow = .Range("B" & .Rows.Count).End(xlUp).Row
    Set copyRange = .Range("B5:B" & lastrow)
End With

With wsInfoTo
    lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row ' <-- different last row in this sheet
    .Range("A1:A" & lastrow).ClearContents

    copyRange.SpecialCells(xlCellTypeVisible).Copy .Range("A" & .Rows.Count).End(xlUp).Offset(3, 0)
End With