简体   繁体   中英

Copy-paste picture with random range in VBA and Excel

Here is my code. I wanted to paste the picture in specific range, i tried different codes but failed. I want it to the top left, Or started in "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

As a recommendation, always try to stay away from Activate , Select and Selection , instead use referenced objects.

The code below will copy the range("B5:G32") from Sheets(3) to Sheets(1) and will place the picture at Cell A1 (top-left position).

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

This should do it

 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

Check a working example for paste as image here http://s000.tinyupload.com/?file_id=04177936342289971269

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM