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

时间:2015-07-07 21:40:26

标签: excel vba excel-vba

我正在尝试选择一系列动态单元格,并将该范围更改并粘贴到另一个工作表上。我已经到了粘贴的地方,但错误地说

  

" Range类的复制方法失败。"

我是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

1 个答案:

答案 0 :(得分:0)

我相信你错误地指定 lr

lr = Range("A" & Rows.Count).End(xlDown).row
'should be...
lr = Range("A" & Rows.Count).End(xlUp).Row

原件将返回1,048,576到 lr