Vba 复制/粘贴-范围类的复制方法失败

Vba 复制/粘贴-范围类的复制方法失败,vba,excel,Vba,Excel,我正在尝试选择一个动态单元格区域,并将更改该区域并将其粘贴到另一张图纸上。我已经到了它粘贴的地方,但是错误是这样说的 “复制范围类的方法失败。” 我是VBA新手,不知道如何解决这个问题。此外,我还希望有一种方法可以在数据粘贴到第二张表中后删除重复项 Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim ws As Worksheet Set ws = Worksheets("Price List") Dim

我正在尝试选择一个动态单元格区域,并将更改该区域并将其粘贴到另一张图纸上。我已经到了它粘贴的地方,但是错误是这样说的

“复制范围类的方法失败。”

我是VBA新手,不知道如何解决这个问题。此外,我还希望有一种方法可以在数据粘贴到第二张表中后删除重复项

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim ws As Worksheet
  Set ws = Worksheets("Price List")
  Dim sel As Worksheet
  Set sel = Worksheets("Selection")
  Dim lr As Long, i As Long
  lr = Range("A" & Rows.Count).End(xlDown).Row
  i = 1

x = 2   'x is the row in the source data
y = 2   'y is the row in the destination area of the worksheet

ws.Range("A2:A" & lr).Copy Destination:=Sheets("Selection").Range("A2") **Problem is right here ^^^**

If Target.Count > 1 Then Exit Sub   'If they highlight a range of cells, don't do anything
Range("c2:J1000").Clear   'Clear the values from the previous range

If Target.Rows > 1 Then   'Don't run code for heading
   Do Until ws.Cells(x, 1) = ""  ' Run until there are no more materials in the source table

      If Cells(Target.Row, 1) = ws.Cells(x, 1) Then    'if the material number is the same as the one clicked on....
         Cells(y, 3) = ws.Cells(x, 1)   'Copy the values to the destination
         Cells(y, 4) = ws.Cells(x, 2)
         Cells(y, 5) = ws.Cells(x, 3)
         Cells(y, 6) = ws.Cells(x, 4)
         Cells(y, 7) = ws.Cells(x, 5)
         Cells(y, 8) = ws.Cells(x, 6)
         Cells(y, 9) = ws.Cells(x, 7)
         Cells(y, 10) = ws.Cells(x, 8)
         y = y + 1
      End If
      x = x + 1
   Loop
End If

Set ac = ActiveSheet.ChartObjects(1)
ac.Chart.ChartTitle.Caption = CStr(Cells(2, 3)) + " (" + CStr(Cells(2, 4)) + ")"

End Sub

我认为您分配的lr不正确

lr = Range("A" & Rows.Count).End(xlDown).row
'should be...
lr = Range("A" & Rows.Count).End(xlUp).Row
原版将返回1048576到lr