将复制的范围粘贴到另一个工作簿表

时间:2016-02-23 16:27:24

标签: excel vba excel-vba

我得到了以下代码,它将一张范围从一张纸粘贴到同一张纸上。

  Sub copyPic()
Dim targetSheet As Worksheet
Set targetSheet = Sheets("Sheet1")

With targetSheet
    .Range("A1:B10") .CopyPicture
    .Paste
    Selection.Name = "pastedPic"

    With .Shapes("pastedPic")
        .Top = targetSheet.Cells(5, 5).Top
        .Left = targetSheet.Cells(5, 5).Left
        .Width = 50
        .Height = 50

    End With
End With

End Sub

我想粘贴" pastedPic"到工作簿(" masterfile.xlsm")。表格(" Page1")到我选择的范围,如果这不起作用那么我想像我一样指定单元格在我的代码中,但这次是在另一个工作簿和另一个工作表中。我想要过的工作簿已经开放。

我该怎么办?

1 个答案:

答案 0 :(得分:0)

您可以使用

 Sheets("Sheet1").Range("A1:B10").CopyPicture
 Dim DestinationSheet as Worksheet
 Set DestinationSheet = WorkBooks("masterfile.xlsm").Sheets ("Sheet1")

 DestinationSheet.Paste 

 Dim pastedPic as Shape

 'The newest picture/shape will always have the index 1

 Set pastedPic = DestinationSheet.Shapes(1)

 With pastedPic 
      .Top = Destination Sheet.Cells(5,5).Top
      'Rest of positioning  code here

 End With