将特殊单元格复制到另一张纸上的下一个空行

时间:2017-03-02 05:15:26

标签: excel vba excel-vba

我正在尝试将带有数字的行复制到另一个工作表。我从另一个论坛获得了SpecialCells代码,但是在将数据粘贴到另一个工作表时遇到了问题。谢谢你的帮助!

这是我的代码:

Sub Sample()

Dim ws As Worksheet
Dim rng As Range
Dim ws1 As Worksheet

On Error GoTo Whoa

Set ws = Sheets("3")
Set ws1 = Sheets("Sheet1")

With ws
    Set rng = .Cells.SpecialCells(xlCellTypeConstants, xlNumbers).EntireRow

    Copy ws1.Range("A" & lastrow)
End With

Exit Sub
End sub

3 个答案:

答案 0 :(得分:1)

你可以试试这个:

Sub Sample()
    With Sheets("Sheet1")
        Sheets("3").UsedRange.SpecialCells(xlCellTypeConstants, xlNumbers).EntireRow.Copy .Cells(.Rows.Count, 1).End(xlUp).Offset(1)
    End With
End sub

答案 1 :(得分:0)

我不会在您的代码中看到您定义和设置lastrow的任何内容。

将您的上一部分修改为:

With ws
    Set Rng = .Cells.SpecialCells(xlCellTypeConstants, xlNumbers).EntireRow

    lastrow = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row + 1 ' <-- get the last row in column A '<-- just in case you are looking for the first empty row in Column A 
    Rng.Copy ws1.Range("A" & lastrow) '<-- you need to add the Rng.Copy
End With

答案 2 :(得分:0)

请尝试下面的代码。这会将名为“3”的工作表中的数据复制到“sheet1”。

Sub Sample()

Dim ws As Worksheet
Dim rng As Range
Dim ws1 As Worksheet

Set ws = Sheets("3")
Set ws1 = Sheets("Sheet1")
LastRow = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row

Set rng = ws.Cells.SpecialCells(xlCellTypeConstants, xlNumbers).EntireRow
rng.Copy
ws1.Activate
Range("A" & LastRow + 1).Select
ActiveSheet.Paste

End Sub