在当前工作表的最后一个活动行之后粘贴来自其他工作表的数据

时间:2018-12-04 01:55:28

标签: excel vba

我想从工作簿的“订单”选项卡的A和B列中复制值,并将这些值粘贴到最后一个活动行之后的“复制警告设置”选项卡中。

Public Sub My_Copy_Orders()
Dim Last_Row As Long
    Sheets("Duplicate warning Settings").Select
    Last_Row = Range("A1").End(xlDown).Offset(1).Row
    Sheets("Orders").Columns("A:B").Copy Destination:=Sheets("Duplicate warning Settings").Range("A" & Last_Row)
End Sub

这给我一个错误

3 个答案:

答案 0 :(得分:0)

您正在复制整列A和B(所有1,048,576行乘以两列)。您的目的地正确地仅使用左上角作为参考点,但是由于您位于第一行下方,因此目的地下方不再有价值1,048,576行的空单元格。本质上,您试图将空白单元格粘贴到工作表底部下方。

只需将副本的来源限制为工作表的usedrange。

with Sheets("Orders")
  intersect(.usedrange, .Columns("A:B")).Copy _
    Destination:=Sheets("Duplicate warning Settings").Range("A" & Last_Row)
end with

答案 1 :(得分:0)

在下面尝试

Public Sub My_Copy_Orders()
Dim Last_Row As Range
Dim sht1, sht2 As Worksheet
Dim aRow, bRow As Long

    Set sht1 = Sheets("Duplicate warning Settings")
    Set sht2 = Sheets("Orders")
    Set Last_Row = sht1.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)

    aRow = sht2.Range("A1").End(xlDown).Row
    bRow = sht2.Range("B1").End(xlDown).Row

        If aRow > bRow Then
            sht2.Range("A1:B" & aRow).Copy Last_Row
        Else
            sht2.Range("A1:B" & bRow).Copy Last_Row
        End If

End Sub

答案 2 :(得分:0)

使用变量的基本代码。

Dim wsSrc As Worksheet, wsDest As Worksheet, scrlRow As Long

Set wsSrc = ThisWorkbook.Sheets("Orders")
Set wsDest = ThisWorkbook.Sheets("Duplicate warning Settings")

srclRow = wsSrc.Cells(Rows.Count, 1).End(xlUp).Row


Dim cpyRng As Range
Set cpyRng = Range("A1:B" & srclRow)

    cpyRng.Copy wsDest.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)