Pao
Pao

Reputation: 1

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

Upvotes: 1

Views: 2555

Answers (2)

Shai Rado
Shai Rado

Reputation: 33672

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

Upvotes: 2

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

Upvotes: 0

Related Questions