在VBA和Excel中使用随机范围复制粘贴图片

时间:2017-01-10 09:56:46

标签: excel vba excel-vba

这是我的代码。我想将图片粘贴到特定范围内,我尝试了不同的代码但失败了。我希望它在左上角,或者在" A1"。

中开始
         Sub CommandButton2_Click()

         ThisWorkbook.Sheets(3).Activate
         ThisWorkbook.Sheets(3).Range("B5:G32").Select
         Selection.CopyPicture Appearance:=xlScreen, Format:=xlBitmap
         Sheet10.Pictures.Paste.Select

         ThisWorkbook.Sheets(1).Activate

         End Sub

2 个答案:

答案 0 :(得分:1)

作为建议,请始终尽量远离ActivateSelectSelection,而应使用引用的对象。

以下代码会将范围(&#34; B5:G32&#34;)从Sheets(3)复制到Sheets(1),并将图片放在单元格A1(左上角位置)。< / p>

Private Sub CommandButton2_Click()

ThisWorkbook.Sheets(3).Range("B5:G32").CopyPicture Appearance:=xlScreen, Format:=xlBitmap

With Sheet10.Pictures.Paste
    .Left = Range("A1").Left ' <-- setting the left postion to Cell A1
    .Top = Range("A1").Top ' <-- setting the top postion to Cell A1
End With

End Sub

答案 1 :(得分:0)

这应该这样做

 Sub CommandButton2_Click()

 ThisWorkbook.Sheets(3).Activate
 Range("B5:G32").Copy  'you can copy directly, in one instruction
 ThisWorkbook.Sheets(10).Activate
 Range("A1").Select  'select where you want to paste it
 ActiveSheet.Pictures.Paste  
 ThisWorkbook.Sheets(1).Activate

 End Sub

在此http://s000.tinyupload.com/?file_id=04177936342289971269

检查粘贴为图像的工作示例