This is code I use in one of my Excel applications. The trick is the AddPicture method that requires a top, left, width and height. These values are picked up from the Range (D2 in this case). Since my image is not the shape of the cell some adjustments need to be made, hence the width times 0.25 and so on.
All four variables are declared as single.
L = .Left + .Width / 4
T = .Top
W = .Width * 0.25
H = .Height * 1.25
End With ' range d2
Call .Shapes.AddPicture("C:\Path\MyImage.Jpg", msoTrue, msoTrue, L, T, W, H)
Place the picture on the sheet and right-click it.Pick Size and Properties.Size it to fit with a cell. Move it to its proper place.Go back to Size and Properties and set the Properties to move and resize with cell.
If you want to move it with code the following will Place the picture then move the picture then remove the picture.
Naming the picture when you place it on the sheet seems to be the easiest way to make a reference to it later, unless someone else has a better way.
Dim MyPath As String
Dim L As Single
Dim T As Single
Dim W As Single
Dim H As Single
Private Sub CommandButton1_Click()
' Place picture on worksheet at a particular cell
MyPath = "C:\MyPath\APicture.jpg"
L = .Left
T = .Top
H = .Height * 2
W = .Width * 2
Call Sheet1.Shapes.AddPicture(MyPath, msoTrue, msoTrue, L, T, W, H)
'If you don't name the picture right here it will be difficult to find later
Sheet1.Shapes.Item(Shapes.Count).Name = "MyPicture"
CommandButton2.Enabled = True
Private Sub CommandButton2_Click()
' Move picture to a given cell
T = .Top
L = .Left
.Top = T
.Left = L
Private Sub CommandButton3_Click()
' Remove picture from sheet
The following code will step through all the shapes on the sheet
See if this helps:
If the current name of the Item is Picture then it makes the picture twice as wide, showing which picture is selected.
An input box collects the name such as RedKingKnight and assigns the name to the Item; then makes the picture the original width.
The good news is you only have to do this once.
Private Sub CommandButton4_Click()
Dim Counter As Integer
Dim NextName As String
For Counter = 1 To Shapes.Count
If Left$(.Item(Counter).Name, 7) = "Picture" Then
.Item(Counter).Width = .Item(Counter).Width * 2
NextName = InputBox("Current Name is" & .Item(Counter).Name, "Rename Pictures")
.Item(Counter).Name = NextName
.Item(Counter).Width = .Item(Counter).Width / 2
I am working creating a fully encapsulated, homogeneous singly linked data structure. The Listing class and SinglyLinkedList class that are part of the whole application compile fine, but the problem ...