OaklandJim
Well-known Member
- Joined
- Nov 29, 2018
- Messages
- 855
- Office Version
- 365
- Platform
- Windows
I have a sub that is 90% there. It copies a picture from sheet 1 and puts it into sheet 2. It sizes the target cell in sheet 2 correctly, based on the cell that the original picture is in. But I cannot put the picture into that target cell in sheet 2. I tried setting the .Left property of the picture based on the .Left property of the target cell. What I am trying is not working. What simple change do I need to make this work?
VBA Code:
Sub CopyEmbedPictureIntoCell()
'
Dim wsFromSheet As Worksheet
Dim wsToSheet As Worksheet
' Cell that the original picture is in.
Dim rFromCell As Range
' Cell into which the copy of the picture is placed.
Dim rToCell As Range
' Note that oPicture and oPicture2 are different types.
Dim oPicture As Shape
Dim oPicture2 As Picture
' Width and height of cells containing pictures.
Dim dColumnWidth As Double
Dim dRowHeight As Double
Set wsFromSheet = Worksheets("Sheet1")
Set wsToSheet = Worksheets("Sheet2")
' Specify cell into which the copy of the original picture is placed.
Set rToCell = wsToSheet.Range("F19")
' Set shape object to the original picture.
Set oPicture = wsFromSheet.Shapes("TestPicture1")
' Set cell that the original picture is in based on TopLeft property
' of the original picture.
Set rFromCell = oPicture.TopLeftCell
' Get width and height of cell containing the original picture.
dColumnWidth = rFromCell.ColumnWidth
dRowHeight = rFromCell.RowHeight
' Size the cell into which the copied picture is placed.
rToCell.ColumnWidth = dColumnWidth
rToCell.RowHeight = dRowHeight
' Copies the original picture (Shape) to the wsToSheet worksheet.
oPicture.Copy
wsToSheet.Paste
Set oPicture2 = Selection
oPicture2.Name = oPicture.Name & "_copy"
oPicture2.Cut
wsToSheet.Paste
' This reports the picture's Left position property
Debug.Print "Picture 2 Left = " & Selection.Left
' Position the copy of the picture in the target cell (rToCell)
' by setting it's Left property.
' These fail
'Selection.Left = rToCell.Left
'Selection.ShapeRange.Left = rToCell.Left
End Sub